import sys from PySide6.QtWidgets import ( QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QFrame, QSizePolicy, ) from PySide6.QtCore import Qt, QTimer, QPoint, QRectF from PySide6.QtGui import ( QFont, QCursor, QPixmap, QImage, QRegion, QPainterPath, QPainter, QColor, ) class LoginWindow(QMainWindow): """登录窗口类,优化宽高比例和布局美观度""" def __init__(self, use_dark_theme=False): super().__init__() # 浅色主题样式表 # 后期写入 .qss 文件 self.light_style = """ QMainWindow, QWidget { background-color: #ffffff; } QLabel#titleLabel { font-weight: bold; color: #2C3E50; /* 工业灰 */ } QLabel#subtitleLabel { font-size: 16px; color: #5f6368; } QLabel#normalLabel { font-size: 14px; color: #5f6368; } QLabel#linkLabel { font-size: 14px; color: #1a73e8; text-decoration: underline; background: transparent; border: none; } QLabel#hintLabel { font-size: 16px; color: white; background-color: #4caf50; border-radius: 8px; padding: 3px 16px; min-height: 29px; min-width: 106px; max-width: 300px; border: none; text-align: center; } QLineEdit { padding: 10px; border: 1px solid #dadce0; border-radius: 6px; font-size: 14px; background-color: white; color: #202124; } QLineEdit:focus { border: 2px solid #1A1A1A; border-radius: 8px; padding: 4px; } QLineEdit::placeholder { color: #424242; } QPushButton { background-color: #1a73e8; color: white; padding: 10px; border-radius: 6px; font-size: 16px; font-weight: 500; } QPushButton:hover { background-color: #1765cc; } QPushButton:pressed { background-color: #165dba; } QFrame#loginFrame { background-color: white; border-top-right-radius: 8px; border-bottom-right-radius: 8px; border: 1px solid #e0e0e0; min-width: 600px; min-height: 360px; max-width: 900px; max-height: 460px; } QWidget#formWidget { background-color: #f8f9fa; border-top-left-radius: 0px; border-bottom-left-radius: 0px; border-top-right-radius: 8px; border-bottom-right-radius: 8px; } QLabel#loginTitleLabel { font-size: 24px; color: #333333; margin-bottom: 16px; background-color: #f8f9fa; } """ # 深色主题样式表 self.dark_style = """ QMainWindow, QWidget { background-color: #1a1a1a; } QLabel#titleLabel { font-weight: bold; color: white; } QLabel#subtitleLabel { font-size: 16px; color: #d0d0d0; } QLabel#normalLabel { font-size: 14px; color: #d0d0d0; } QLabel#linkLabel { font-size: 14px; color: #8ab4f8; text-decoration: underline; background: transparent; border: none; } QLabel#hintLabel { font-size: 16px; color: white; background-color: #4caf50; border-radius: 8px; padding: 3px 16px; min-height: 29px; min-width: 106px; max-width: 300px; border: none; text-align: center; } QLineEdit { padding: 10px; border: 1px solid #444444; border-radius: 6px; font-size: 14px; background-color: #333333; color: #e0e0e0; } QLineEdit:focus { border: 2px solid #8ab4f8; outline: none; } QPushButton { background-color: #1a73e8; color: white; padding: 10px; border-radius: 6px; font-size: 16px; font-weight: 500; } QPushButton:hover { background-color: #2962ff; } QPushButton:pressed { background-color: #1565c0; } QFrame#loginFrame { background-color: #2d2d2d; border: 1px solid #444444; border-top-right-radius: 8px; border-bottom-right-radius: 8px; min-width: 600px; min-height: 360px; max-width: 900px; max-height: 460px; } QWidget#formWidget { background-color: #e9e9e9; border-top-left-radius: 0px; border-bottom-left-radius: 0px; border-top-right-radius: 8px; border-bottom-right-radius: 8px; } QLabel#loginTitleLabel { font-size: 24px; color: #333333; margin-bottom: 16px; background-color: #e9e9e9; } """ # 可以根据不同的主题,设置不同的样式表 if use_dark_theme: self.setStyleSheet(self.dark_style) else: self.setStyleSheet(self.light_style) self.init_ui() def init_ui(self): self.setWindowTitle("密胺餐盘自动化生产控制系统") self.resize(900, 600) # 3:2比例(900/600=1.5) self.setMinimumSize(750, 500) # 最小尺寸保持3:2比例 # 创建中心部件和主布局 central_widget = QWidget() self.setCentralWidget(central_widget) main_layout = QVBoxLayout(central_widget) main_layout.setContentsMargins(50, 10, 50, 50) main_layout.setSpacing(15) # 提示信息容器 hint_container = QWidget() hint_container.setFixedHeight(50) hint_layout = QHBoxLayout(hint_container) hint_layout.setContentsMargins(0, 0, 0, 0) hint_layout.setAlignment(Qt.AlignCenter) self.hint_label = QLabel("密码: 123") self.hint_label.setObjectName("hintLabel") self.hint_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.hint_label.setVisible(False) hint_layout.addWidget(self.hint_label) main_layout.addWidget(hint_container) # 系统标题(随窗口动态缩放) self.title_label = QLabel("密胺餐盘自动化生产控制系统") self.title_label.setObjectName("titleLabel") self.title_label.setAlignment(Qt.AlignCenter) main_layout.addWidget(self.title_label) # 系统描述 subtitle_label = QLabel("高效、安全的自动化控制系统") subtitle_label.setObjectName("subtitleLabel") subtitle_label.setAlignment(Qt.AlignCenter) subtitle_label.setFixedHeight(25) main_layout.addWidget(subtitle_label) main_layout.addSpacing(20) # ---------------- 登录框:左图 + 右表单 ---------------- # -------- 登录框 占比:左图(1份) + 右表单(1份) -------- login_frame = QFrame() login_frame.setObjectName("loginFrame") login_layout = QHBoxLayout(login_frame) login_layout.setSpacing(0) # 左右紧贴,无间距 login_layout.setContentsMargins(0, 0, 0, 0) # 左上右下内边距为0 # -------- 左侧图片区域(占1份宽度) -------- self.image_label = QLabel() self.image_label.setSizePolicy( QSizePolicy.Expanding, QSizePolicy.Expanding # 水平拉伸 # 垂直拉伸 ) self.image_label.setAlignment(Qt.AlignCenter) # 加载左侧图片(png图片) image_path = r".\resource\login.png" pixmap = QPixmap(image_path) if not pixmap.isNull(): self.image_label.setPixmap(pixmap) self.image_label.setScaledContents(True) # 填充容器,保持比例 else: self.image_label.setText("图片加载失败") self.image_label.setStyleSheet("font-size: 14px; color: #888;") login_layout.addWidget(self.image_label, stretch=1) # 图片占1份 # -------- 右侧表单区域(占2份宽度,内容居中) -------- form_widget = QWidget() form_widget.setObjectName("formWidget") form_widget.setContentsMargins(30, 30, 30, 30) form_layout = QVBoxLayout(form_widget) form_layout.setSpacing(25) form_layout.setContentsMargins(0, 0, 0, 0) # 上左下右内边距为0 form_layout.setAlignment(Qt.AlignCenter) # 表单内容居中 self.login_title_label = QLabel("Sign In") # 登录标题文本 self.login_title_label.setObjectName("loginTitleLabel") # 账号输入 self.username_input = QLineEdit() self.username_input.setPlaceholderText("请输入账号") self.username_input.setText("manager") self.username_input.setFixedWidth(250) # 固定宽度,避免过度拉伸 # 密码输入 self.password_input = QLineEdit() self.password_input.setPlaceholderText("请输入密码") self.password_input.setEchoMode(QLineEdit.Password) self.password_input.setFixedWidth(250) # 登录按钮 self.login_btn = QPushButton("登录") self.login_btn.clicked.connect(self.validate_login) # 用户登录验证 self.login_btn.setMinimumWidth(120) self.login_btn.setMaximumWidth(200) # 登录按钮样式 self.login_btn.setStyleSheet( """ QPushButton { background-color: #4A6FA5; color: white; padding: 6px 10px; border-radius: 15px; font-size: 16px; font-weight: 550; min-height: 24px; max-height: 32px; } QPushButton:hover { background-color: #3D5D8A; } QPushButton:pressed { background-color: #304C70; } """ ) # 忘记密码链接 forgot_layout = QHBoxLayout() forgot_layout.setAlignment(Qt.AlignCenter) # 忘记密码居中 self.forgot_password = QLabel("忘记密码") self.forgot_password.setObjectName("linkLabel") self.forgot_password.setTextInteractionFlags(Qt.TextSelectableByMouse) self.forgot_password.setCursor(QCursor(Qt.PointingHandCursor)) self.forgot_password.mousePressEvent = self.show_password_hint forgot_layout.addWidget(self.forgot_password) # forgot_layout.addStretch() # 组装表单布局 # 添加到布局时显式指定水平居中 form_layout.addWidget(self.login_title_label, alignment=Qt.AlignHCenter) form_layout.addWidget(self.username_input, alignment=Qt.AlignHCenter) form_layout.addWidget(self.password_input, alignment=Qt.AlignHCenter) form_layout.addWidget(self.login_btn, alignment=Qt.AlignHCenter) form_layout.addLayout(forgot_layout) login_layout.addWidget(form_widget, stretch=1) # 表单占1份 # -------- 登录框加入主布局 -------- center_layout = QHBoxLayout() # 使登录框居中显示 center_layout.addStretch(2) center_layout.addWidget(login_frame, stretch=3) # 登录框整体占3份 center_layout.addStretch(2) main_layout.addLayout(center_layout, stretch=2) main_layout.addStretch(1) # 默认选中密码框 self.password_input.setFocus() # 提示定时器 self.hint_timer = QTimer(self) self.hint_timer.setSingleShot(True) self.hint_timer.timeout.connect(self.hide_password_hint) # 初始化标题大小 self.adjust_title_size() def adjust_title_size(self): """调整标题大小,适应新的宽高比例""" window_width = self.width() font_size = max(20, min(36, window_width // 60)) # 除数增大,字体增长更平缓 font = QFont() font.setPointSize(font_size) self.title_label.setFont(font) def resizeEvent(self, event): """窗口大小改变时调整标题""" super().resizeEvent(event) self.adjust_title_size() def toggle_theme(self): """切换主题""" current_style = self.styleSheet() if current_style == self.light_style: self.setStyleSheet(self.dark_style) else: self.setStyleSheet(self.light_style) def show_password_hint(self, event): self.hint_label.setVisible(True) self.hint_timer.start(3000) def hide_password_hint(self): self.hint_label.setVisible(False) # 目前写死,密码都是123 # 后续 可以从数据库读入 def validate_login(self): username = self.username_input.text().strip() password = self.password_input.text().strip() if password != "123": QMessageBox.warning(self, "登录失败", "密码错误,请重试!") return # 根据登录的用户的不同,跳转到不同的界面 if username == "manager": self.open_workshop_manager_interface() elif username == "admin": self.open_debugger_interface() else: QMessageBox.warning(self, "登录失败", "用户名不存在!") return # 测试界面接口一 def open_workshop_manager_interface(self): current_style = self.styleSheet() self.workshop_window = WorkshopManagerWindow(current_style) self.workshop_window.show() self.close() # 测试界面接口二 def open_debugger_interface(self): current_style = self.styleSheet() self.debugger_window = DebuggerWindow(current_style) self.debugger_window.show() self.close() class WorkshopManagerWindow(QMainWindow): def __init__(self, style_sheet): super().__init__() self.setStyleSheet(style_sheet) self.init_ui() def init_ui(self): self.setWindowTitle("车间主管控制台 - 密胺餐盘自动化生产控制系统") self.resize(1050, 700) # 3:2比例 self.setMinimumSize(900, 600) central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) title_label = QLabel("欢迎使用车间主管系统") title_label.setObjectName("titleLabel") title_label.setAlignment(Qt.AlignCenter) title_label.setFont(QFont("Arial", 18, QFont.Bold)) info_label = QLabel("这里是密胺餐盘生产线的管理和监控功能界面") info_label.setObjectName("normalLabel") info_label.setAlignment(Qt.AlignCenter) layout.addStretch() layout.addWidget(title_label) layout.addWidget(info_label) layout.addStretch() class DebuggerWindow(QMainWindow): def __init__(self, style_sheet): super().__init__() self.setStyleSheet(style_sheet) self.init_ui() def init_ui(self): self.setWindowTitle("调试人员控制台 - 密胺餐盘自动化生产控制系统") self.resize(1050, 700) # 3:2比例 self.setMinimumSize(900, 600) central_widget = QWidget() self.setCentralWidget(central_widget) layout = QVBoxLayout(central_widget) title_label = QLabel("欢迎使用调试人员系统") title_label.setObjectName("titleLabel") title_label.setAlignment(Qt.AlignCenter) title_label.setFont(QFont("Arial", 18, QFont.Bold)) info_label = QLabel("这里是密胺餐盘生产设备的调试和参数配置界面") info_label.setObjectName("normalLabel") info_label.setAlignment(Qt.AlignCenter) layout.addStretch() layout.addWidget(title_label) layout.addWidget(info_label) layout.addStretch() if __name__ == "__main__": app = QApplication(sys.argv) window = LoginWindow() window.show() sys.exit(app.exec())