Files
Feeding_control_system/view/widgets/system_button_widget.py

76 lines
2.6 KiB
Python
Raw Normal View History

2025-10-18 18:29:40 +08:00
from PySide6.QtWidgets import QApplication, QPushButton, QHBoxLayout, QWidget
from PySide6.QtGui import QIcon
from PySide6.QtCore import QSize, Qt
import sys
import resources.resource_rc
class SystemButtonWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建水平布局并设置按钮间距为27px
layout = QHBoxLayout()
layout.setSpacing(27) # 按钮之间的间距
layout.setContentsMargins(6, 6, 6, 6) # 布局边缘留白
self.setWindowTitle("系统按钮")
self.setFixedSize(776, 65) # widget大小
# 按钮样式表
button_style = """
QPushButton {
width: 120px; /* 宽120px */
height: 40px; /* 高40px */
background-color: #7f95bf; /* 平时颜色 */
color: white; /* 文字颜色白色更清晰 */
font-size: 20px; /* 字号20px */
border: none; /* 去掉默认边框 */
border-radius: 4px; /* 轻微圆角可选更美观 */
}
QPushButton:hover {
background-color: #2573d3; /* 鼠标悬停颜色 */
}
QPushButton:pressed {
background-color: #1d5cb8; /* 点击时颜色(可选增强) */
}
"""
# 创建按钮列表(图标+文字)
# 注意:需要替换为实际的图片路径
buttons_info = [
("系统启动", ":/icons/images/系统启动.png"),
("系统停止", ":/icons/images/系统停止.png"),
("系统复位", ":/icons/images/系统复位.png"),
("AI检测", ":/icons/images/AI检测.png"),
("任务刷新", ":/icons/images/任务刷新.png")
]
# 存放按钮控件的字典
self.buttons = dict()
for text, icon_path in buttons_info:
btn = QPushButton(text)
# 设置图标
icon = QIcon(icon_path)
btn.setIcon(icon)
btn.setIconSize(QSize(24, 24)) # 图标尺寸(与按钮比例协调)
# 设置固定尺寸(确保宽高生效)
btn.setFixedSize(120, 40)
# 应用样式
btn.setStyleSheet(button_style)
# 添加到布局
layout.addWidget(btn)
self.buttons[text] = btn
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = SystemButtonWidget()
window.show()
sys.exit(app.exec())