为多个控件统一设置样式
可以将样式字符串定义一次,然后将其应用到每个控件上。这样,就不需要分别设置样式了。
以按钮为例,上代码:
from PyQt5.QtWidgets import QPushButton
# 定义按钮样式
button_style = """
QPushButton {
border: 2px solid gray; /* 边框颜色和粗细 */
border-radius: 5px; /* 边框圆角 */
padding: 5px 10px; /* 内边距 */
background-color: #f0f0f0; /* 背景颜色 */
color: black; /* 文字颜色 */
font-size: 12px; /* 字体大小 */
}
QPushButton:hover {
background-color: #e0e0e0; /* 鼠标悬停时的背景颜色 */
}
"""
# 创建按钮并设置样式
a = QP