Files
zjsh_weight_system_ui/system_nav_bar.py

141 lines
5.4 KiB
Python
Raw Normal View History

2025-11-16 11:58:22 +08:00
from PySide6.QtWidgets import (QWidget, QLabel, QHBoxLayout,
QSpacerItem, QSizePolicy, QPushButton)
from PySide6.QtGui import QPixmap, QFont, QPainter, QIcon
from PySide6.QtCore import Qt, QTimer, QDateTime
# 自定义消息容器, 显示系统消息
class MsgContainer(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(770, 24)
# 加载消息区域背景图
self.bg_pixmap = QPixmap("系统消息背景.png") # 替换为实际路径
if self.bg_pixmap.isNull():
print("警告:系统消息背景.png 加载失败")
# 消息区域内部布局(喇叭+文本)
msg_layout = QHBoxLayout(self)
msg_layout.setContentsMargins(0, 0, 0, 0) # 调整内边距,避免内容贴边
msg_layout.setSpacing(3) # 喇叭和文本的间距
# 消息喇叭图标
self.msg_icon = QLabel()
self.msg_icon.setFixedSize(13, 18)
# self.msg_icon.setStyleSheet("background-color:red;")
self.msg_icon.setPixmap(QPixmap("系统消息喇叭.png")) # 替换为实际路径
msg_layout.addWidget(self.msg_icon, alignment=Qt.AlignVCenter | Qt.AlignLeft)
# 消息文本
current_time = QDateTime.currentDateTime().toString("hh:mm:ss")
self.msg_text = QLabel(f"{current_time} 开始启动智能浇筑系统")
self.msg_text.setFixedWidth(740)
# self.msg_text.setStyleSheet("color: white; font-size: 14px;background-color:red;") # 文本样式
self.msg_text.setStyleSheet("color: white; font-size: 16px;font-weight:Bold;")
self.msg_text.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
msg_layout.addWidget(self.msg_text)
# 关闭按钮
self._create_close_button(msg_layout)
def _create_close_button(self, parent_layout):
self.close_btn = QPushButton()
self.close_btn.setFixedSize(20, 20)
close_icon = QPixmap("关闭图标.png")
if not close_icon.isNull():
self.close_btn.setIcon(QIcon(close_icon))
self.close_btn.setStyleSheet(
"""
QPushButton {
background-color: transparent;
border: none;
padding: 0px;
}
QPushButton:hover {
background-color: red;
border-radius: 2px;
}
"""
)
parent_layout.addWidget(self.close_btn)
def paintEvent(self, event):
# 绘制消息区域背景图
super().paintEvent(event) # 确保子控件正常绘制
if self.bg_pixmap.isNull():
return # 图片加载失败则不绘制
painter = QPainter(self)
painter.setRenderHint(QPainter.SmoothPixmapTransform) # 缩放平滑
painter.drawPixmap(self.rect(), self.bg_pixmap)
class SystemNavBar(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# 设置尺寸
self.setFixedSize(1280, 80)
# 1. 加载背景图
self.bg_pixmap = QPixmap("系统主界面导航栏.png") # 替换为实际图片路径
if self.bg_pixmap.isNull():
print("警告:背景图加载失败,请检查路径!")
main_layout = QHBoxLayout(self)
main_layout.setContentsMargins(9, 9, 9, 19)
main_layout.setSpacing(100) # 注意左侧的logo+系统标题的容器 和 系统消息的间隔
# 左侧区域logo + 系统标题
left_container = QWidget()
left_container.setFixedSize(400, 53)
left_layout = QHBoxLayout(left_container) # 容器内部的水平布局
left_layout.setContentsMargins(0, 0, 0, 0) # 容器内边距
left_layout.setSpacing(6) # 设置logo和标题之间的间隙为6px
# 系统logo
self.logo = QLabel()
self.logo.setFixedSize(53, 53)
self.logo.setPixmap(QPixmap("系统logo.png"))
left_layout.addWidget(self.logo, alignment=Qt.AlignTop)
# 系统总标题
self.title = QLabel()
self.title.setPixmap(QPixmap("系统总标题.png"))
left_layout.addWidget(self.title, alignment=Qt.AlignCenter)
main_layout.addWidget(left_container, alignment=Qt.AlignTop)
# 中间区域:系统消息(喇叭+文本+背景)
self.msg_container = MsgContainer()
main_layout.addWidget(self.msg_container, alignment=Qt.AlignBottom | Qt.AlignRight)
# 右侧区域:实时时间
self.time_label = QLabel()
self.time_label.setStyleSheet("color: white; font-size: 16px;font-weight:Bold;")
main_layout.addWidget(self.time_label, alignment= Qt.AlignTop | Qt.AlignRight)
# 启动时间更新定时器
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_time)
self.timer.start(1000) # 每秒更新一次
def paintEvent(self, event):
super().paintEvent(event)
painter = QPainter(self)
painter.setRenderHint(QPainter.SmoothPixmapTransform) # 缩放时平滑
painter.drawPixmap(0, 0, self.bg_pixmap)
def update_time(self):
current_time = QDateTime.currentDateTime().toString("yyyy/MM/dd hh:mm:ss")
self.time_label.setText(current_time)
# 测试代码
if __name__ == "__main__":
import sys
from PySide6.QtWidgets import QApplication, QMainWindow
app = QApplication(sys.argv)
nav_bar = SystemNavBar()
nav_bar.show()
sys.exit(app.exec())