V1.0
This commit is contained in:
377
main.py
Normal file
377
main.py
Normal file
@ -0,0 +1,377 @@
|
|||||||
|
import sys
|
||||||
|
from PySide6.QtWidgets import (
|
||||||
|
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
|
||||||
|
QLabel, QFrame, QSizePolicy, QPushButton
|
||||||
|
)
|
||||||
|
from PySide6.QtCore import Qt, QTimer
|
||||||
|
from PySide6.QtGui import QFont
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
class StatusMonitor(QWidget):
|
||||||
|
"""
|
||||||
|
中交三航精准布料浇筑要料系统 - 主界面类
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
"""构造函数:初始化主界面的UI布局、控件和定时器"""
|
||||||
|
super().__init__(parent=parent)
|
||||||
|
self.is_running = False # 系统运行状态标记
|
||||||
|
|
||||||
|
# 窗口基础设置
|
||||||
|
self.setWindowTitle("中交三航精准布料浇筑要料系统")
|
||||||
|
self.setGeometry(100, 100, 800, 500) # 设置窗口位置和大小
|
||||||
|
|
||||||
|
# 初始化主布局(垂直布局)
|
||||||
|
self.mainLayout = QVBoxLayout(self)
|
||||||
|
self.mainLayout.setContentsMargins(10, 10, 10, 10) # 设置布局的内边距
|
||||||
|
self.mainLayout.setSpacing(10) # 相邻控件的间距
|
||||||
|
|
||||||
|
# 初始化界面组件
|
||||||
|
self._init_title_label()
|
||||||
|
self._init_time_label()
|
||||||
|
self._init_status_container()
|
||||||
|
self._init_timers()
|
||||||
|
|
||||||
|
def _init_title_label(self):
|
||||||
|
"""初始化系统标题标签"""
|
||||||
|
titleLabel = QLabel("中交三航精准布料浇筑要料系统")
|
||||||
|
titleLabel.setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
background-color: #66BB6A;
|
||||||
|
color: white;
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: bold;
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 2px solid #E8F5E9;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
titleLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
titleLabel.setFont(QFont("Microsoft YaHei", 24, QFont.Bold))
|
||||||
|
self.mainLayout.addWidget(titleLabel)
|
||||||
|
|
||||||
|
def _init_time_label(self):
|
||||||
|
"""初始化实时时间显示标签"""
|
||||||
|
self.timeLabel = QLabel(self.get_current_time())
|
||||||
|
self.timeLabel.setStyleSheet("""
|
||||||
|
background-color: #E8F5E9;
|
||||||
|
color: #2E7D32;
|
||||||
|
font-size: 16px;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 6px;
|
||||||
|
border: 1px solid #C8E6C9;
|
||||||
|
""")
|
||||||
|
self.timeLabel.setFont(QFont("Arial", weight=QFont.Bold))
|
||||||
|
self.timeLabel.setAlignment(Qt.AlignmentFlag.AlignRight)
|
||||||
|
self.timeLabel.setSizePolicy(
|
||||||
|
QSizePolicy.Policy.Expanding, # 水平方向尽可能的占据布局中可用的空间
|
||||||
|
QSizePolicy.Policy.Fixed # 垂直方向固定为固定高度
|
||||||
|
)
|
||||||
|
self.mainLayout.addWidget(self.timeLabel, 0)
|
||||||
|
|
||||||
|
def _init_status_container(self):
|
||||||
|
"""初始化核心状态监控容器(包含状态组和操作按钮)"""
|
||||||
|
self.bigContainer = QFrame()
|
||||||
|
self.bigContainer.setStyleSheet("""
|
||||||
|
QFrame {
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
border: 2px solid #cccccc;
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
self.bigContainer.setSizePolicy(
|
||||||
|
QSizePolicy.Policy.Expanding,
|
||||||
|
QSizePolicy.Policy.Expanding
|
||||||
|
)
|
||||||
|
|
||||||
|
containerLayout = QVBoxLayout(self.bigContainer)
|
||||||
|
containerLayout.setContentsMargins(20, 20, 20, 20)
|
||||||
|
|
||||||
|
self._init_status_groups(containerLayout)
|
||||||
|
self._init_operation_buttons(containerLayout)
|
||||||
|
|
||||||
|
self.mainLayout.addWidget(self.bigContainer, 1)
|
||||||
|
|
||||||
|
def _init_status_groups(self, parent_layout):
|
||||||
|
"""初始化左右状态组(包含4个状态项)"""
|
||||||
|
statusWidget = QWidget()
|
||||||
|
statusLayout = QHBoxLayout(statusWidget)
|
||||||
|
statusLayout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
statusLayout.setSpacing(30)
|
||||||
|
|
||||||
|
leftGroup = QWidget()
|
||||||
|
leftLayout = QVBoxLayout(leftGroup)
|
||||||
|
leftLayout.setSpacing(15)
|
||||||
|
leftLayout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
|
||||||
|
rightGroup = QWidget()
|
||||||
|
rightLayout = QVBoxLayout(rightGroup)
|
||||||
|
rightLayout.setSpacing(15)
|
||||||
|
rightLayout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
|
||||||
|
statusNames = ["盖板状态", "液压对齐状态", "LED状态", "按键状态"]
|
||||||
|
self.statusWidgets = []
|
||||||
|
|
||||||
|
for i, name in enumerate(statusNames):
|
||||||
|
statusItem = QFrame()
|
||||||
|
statusItem.setStyleSheet("""
|
||||||
|
QFrame {
|
||||||
|
background-color: white;
|
||||||
|
border: 1px solid #d0d0d0;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 10px;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
statusItem.setFixedHeight(80)
|
||||||
|
|
||||||
|
itemLayout = QHBoxLayout(statusItem)
|
||||||
|
itemLayout.setContentsMargins(10, 5, 10, 5)
|
||||||
|
|
||||||
|
# 状态指示灯
|
||||||
|
indicator = QLabel()
|
||||||
|
indicator.setFixedSize(20, 20)
|
||||||
|
indicator.setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
background-color: #9E9E9E;
|
||||||
|
border-radius: 10px;
|
||||||
|
border: 2px solid #757575;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
|
# 状态名称标签
|
||||||
|
nameLabel = QLabel(name)
|
||||||
|
nameLabel.setStyleSheet("font-size: 14px; color: #333333;")
|
||||||
|
nameLabel.setFont(QFont("Microsoft YaHei", 12))
|
||||||
|
|
||||||
|
# 状态值标签
|
||||||
|
valueLabel = QLabel("否")
|
||||||
|
valueLabel.setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #D32F2F;
|
||||||
|
background-color: #FFEBEE;
|
||||||
|
padding: 5px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
border: 1px solid #FFCDD2;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
valueLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
valueLabel.setMinimumWidth(60)
|
||||||
|
|
||||||
|
itemLayout.addWidget(indicator)
|
||||||
|
itemLayout.addSpacing(10)
|
||||||
|
itemLayout.addWidget(nameLabel)
|
||||||
|
itemLayout.addStretch()
|
||||||
|
itemLayout.addWidget(valueLabel)
|
||||||
|
|
||||||
|
self.statusWidgets.append({
|
||||||
|
'indicator': indicator,
|
||||||
|
'nameLabel': nameLabel,
|
||||||
|
'valueLabel': valueLabel,
|
||||||
|
'status': False
|
||||||
|
})
|
||||||
|
|
||||||
|
if i % 2 == 0:
|
||||||
|
leftLayout.addWidget(statusItem)
|
||||||
|
else:
|
||||||
|
rightLayout.addWidget(statusItem)
|
||||||
|
|
||||||
|
statusLayout.addWidget(leftGroup)
|
||||||
|
statusLayout.addStretch(1)
|
||||||
|
statusLayout.addWidget(rightGroup)
|
||||||
|
|
||||||
|
parent_layout.addWidget(statusWidget)
|
||||||
|
|
||||||
|
def _init_operation_buttons(self, parent_layout):
|
||||||
|
"""初始化操作按钮(开始/重启/停止)"""
|
||||||
|
buttonContainer = QWidget()
|
||||||
|
buttonLayout = QHBoxLayout(buttonContainer)
|
||||||
|
buttonLayout.setContentsMargins(0, 20, 0, 0)
|
||||||
|
buttonLayout.setSpacing(30)
|
||||||
|
|
||||||
|
self.startButton = QPushButton("开始")
|
||||||
|
self.restartButton = QPushButton("重启")
|
||||||
|
self.stopButton = QPushButton("停止")
|
||||||
|
|
||||||
|
button_base_style = """
|
||||||
|
QPushButton {
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: bold;
|
||||||
|
padding: 10px 20px;
|
||||||
|
border-radius: 6px;
|
||||||
|
border: 1px solid;
|
||||||
|
min-width: 100px;
|
||||||
|
}
|
||||||
|
QPushButton:hover {
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
QPushButton:pressed {
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.startButton.setStyleSheet(button_base_style + """
|
||||||
|
QPushButton {
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
border-color: #2E7D32;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
|
self.restartButton.setStyleSheet(button_base_style + """
|
||||||
|
QPushButton {
|
||||||
|
background-color: #FF9800;
|
||||||
|
color: white;
|
||||||
|
border-color: #F57C00;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
|
self.stopButton.setStyleSheet(button_base_style + """
|
||||||
|
QPushButton {
|
||||||
|
background-color: #F44336;
|
||||||
|
color: white;
|
||||||
|
border-color: #D32F2F;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
|
button_font = QFont("Microsoft YaHei", 12, QFont.Bold)
|
||||||
|
self.startButton.setFont(button_font)
|
||||||
|
self.restartButton.setFont(button_font)
|
||||||
|
self.stopButton.setFont(button_font)
|
||||||
|
|
||||||
|
self.startButton.clicked.connect(self.on_start_clicked)
|
||||||
|
self.restartButton.clicked.connect(self.on_restart_clicked)
|
||||||
|
self.stopButton.clicked.connect(self.on_stop_clicked)
|
||||||
|
|
||||||
|
buttonLayout.addStretch(1)
|
||||||
|
buttonLayout.addWidget(self.startButton)
|
||||||
|
buttonLayout.addWidget(self.restartButton)
|
||||||
|
buttonLayout.addWidget(self.stopButton)
|
||||||
|
buttonLayout.addStretch(1)
|
||||||
|
|
||||||
|
parent_layout.addWidget(buttonContainer)
|
||||||
|
|
||||||
|
def _init_timers(self):
|
||||||
|
"""初始化定时器(时间更新+状态模拟)"""
|
||||||
|
# 时间更新定时器(每秒更新一次)
|
||||||
|
self.time_timer = QTimer()
|
||||||
|
self.time_timer.timeout.connect(self.update_time)
|
||||||
|
self.time_timer.start(1000)
|
||||||
|
|
||||||
|
# 状态模拟定时器(每3秒更新一次)
|
||||||
|
self.status_timer = QTimer()
|
||||||
|
self.status_timer.timeout.connect(self.simulate_mcu_query)
|
||||||
|
self.status_timer.start(3000)
|
||||||
|
|
||||||
|
def get_current_time(self):
|
||||||
|
"""获取格式化的当前时间"""
|
||||||
|
return f"🕒 当前时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
||||||
|
|
||||||
|
def update_time(self):
|
||||||
|
"""更新时间显示"""
|
||||||
|
self.timeLabel.setText(self.get_current_time())
|
||||||
|
|
||||||
|
def simulate_mcu_query(self):
|
||||||
|
"""模拟单片机状态查询"""
|
||||||
|
import random # 仅用于模拟,实际项目替换为真实通信逻辑
|
||||||
|
|
||||||
|
if self.is_running:
|
||||||
|
for widget in self.statusWidgets:
|
||||||
|
status = random.choice([True, False])
|
||||||
|
widget['status'] = status
|
||||||
|
|
||||||
|
if status:
|
||||||
|
# 状态为"是"时的样式
|
||||||
|
widget['valueLabel'].setText("是")
|
||||||
|
widget['valueLabel'].setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #2E7D32;
|
||||||
|
background-color: #E8F5E9;
|
||||||
|
padding: 5px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
border: 1px solid #C8E6C9;
|
||||||
|
min-width: 60px;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
widget['indicator'].setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
background-color: #4CAF50;
|
||||||
|
border-radius: 10px;
|
||||||
|
border: 2px solid #2E7D32;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
else:
|
||||||
|
# 状态为"否"时的样式
|
||||||
|
widget['valueLabel'].setText("否")
|
||||||
|
widget['valueLabel'].setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #D32F2F;
|
||||||
|
background-color: #FFEBEE;
|
||||||
|
padding: 5px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
border: 1px solid #FFCDD2;
|
||||||
|
min-width: 60px;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
widget['indicator'].setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
background-color: #F44336;
|
||||||
|
border-radius: 10px;
|
||||||
|
border: 2px solid #D32F2F;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
|
def on_start_clicked(self):
|
||||||
|
"""开始按钮点击事件"""
|
||||||
|
if not self.is_running:
|
||||||
|
self.is_running = True
|
||||||
|
print("系统已启动,开始监控状态...")
|
||||||
|
self.simulate_mcu_query() # 启动时立即更新一次状态
|
||||||
|
|
||||||
|
def on_stop_clicked(self):
|
||||||
|
"""停止按钮点击事件"""
|
||||||
|
if self.is_running:
|
||||||
|
self.is_running = False
|
||||||
|
print("系统已停止,状态监控暂停...")
|
||||||
|
|
||||||
|
def on_restart_clicked(self):
|
||||||
|
"""重启按钮点击事件"""
|
||||||
|
print("系统正在重启...")
|
||||||
|
self.is_running = False
|
||||||
|
# 重置所有状态为"否"
|
||||||
|
for widget in self.statusWidgets:
|
||||||
|
widget['status'] = False
|
||||||
|
widget['valueLabel'].setText("否")
|
||||||
|
widget['valueLabel'].setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #D32F2F;
|
||||||
|
background-color: #FFEBEE;
|
||||||
|
padding: 5px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
border: 1px solid #FFCDD2;
|
||||||
|
min-width: 60px;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
widget['indicator'].setStyleSheet("""
|
||||||
|
QLabel {
|
||||||
|
background-color: #F44336;
|
||||||
|
border-radius: 10px;
|
||||||
|
border: 2px solid #D32F2F;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
# 重启后自动开始
|
||||||
|
QTimer.singleShot(1000, self.on_start_clicked)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
window = StatusMonitor()
|
||||||
|
window.show()
|
||||||
|
sys.exit(app.exec())
|
||||||
Reference in New Issue
Block a user