add tests (在测试模块,增加 emv界面测试 和 计量界面测试)

This commit is contained in:
2025-08-29 14:55:00 +08:00
parent 7cb36c75ea
commit 17abd13df7
8 changed files with 407 additions and 0 deletions

17
tests/common.py Normal file
View File

@ -0,0 +1,17 @@
import sys
import os
# 注意: 只在 tests 目录下的 测试文件中使用
# 设置项目目录 为搜索目录
# 通用逻辑:添加项目根目录到搜索路径
def add_project_root_to_path():
# 获取项目根目录tests的上级目录
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if project_root not in sys.path:
sys.path.append(project_root)
# 自动执行(导入时就添加路径)
add_project_root_to_path()

234
tests/test_emv.py Normal file
View File

@ -0,0 +1,234 @@
from common import *
from PySide6.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QHBoxLayout,
QSpacerItem,
QSizePolicy,
QLabel,
)
from PySide6.QtCore import Qt, QTimer, Signal
from qfluentwidgets import (
PushButton,
LineEdit,
BodyLabel,
MessageBox,
setTheme,
Theme,
)
import sys
class DOWidget(QWidget):
# open开关按下
open_btn_clicked_sig = Signal()
# close开关按下
close_btn_clicked_sig = Signal()
def __init__(self, title: str, parent=None):
super().__init__(parent)
self.title = title # DO名称如"DO1"
self.is_open = False # 初始状态:关闭
self.__initWidget()
def __initWidget(self):
# 创建控件
self.__createWidget()
# 设置样式
self.__initStyles()
# 设置布局
self.__initLayout()
# 绑定
self.__bind()
def __createWidget(self):
# Do按钮名称的 label
self.title_label = BodyLabel(self.title)
self.title_label.setAlignment(Qt.AlignCenter)
# 状态指示灯
self.status_light = QLabel()
self.status_light.setFixedSize(40, 40)
# 打开和关闭按钮
self.open_btn = PushButton("打开")
self.close_btn = PushButton("关闭")
def __initStyles(self):
# 状态指示灯样式(初始颜色:灰色(关闭))
self.status_light.setStyleSheet(
"""
border-radius: 20px;
background-color: gray;
"""
)
def __initLayout(self):
# 垂直布局:标题 → 状态灯 → 按钮
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(15, 15, 15, 15) # 内边距
main_layout.setSpacing(12) # 控件间距
# 按钮布局
btn_layout = QHBoxLayout()
btn_layout.setSpacing(8)
btn_layout.addWidget(self.open_btn)
btn_layout.addWidget(self.close_btn)
main_layout.addWidget(self.title_label) # 添加按钮名称
main_layout.addWidget(self.status_light, alignment=Qt.AlignCenter) # 添加指示灯
main_layout.addLayout(btn_layout)
def __bind(self):
self.open_btn.clicked.connect(self.open_btn_clicked_sig)
self.close_btn.clicked.connect(self.close_btn_clicked_sig)
# def on_open(self):
# """打开按钮点击事件:更新状态灯为绿色"""
# self.is_open = True
# self.status_light.setStyleSheet(
# """
# border-radius: 20px;
# background-color: green; /* 打开 → 绿色 */
# """
# )
# def on_close(self):
# """关闭按钮点击事件:更新状态灯为灰色"""
# self.is_open = False
# self.status_light.setStyleSheet(
# """
# border-radius: 20px;
# background-color: gray; /* 关闭 → 灰色 */
# """
# )
class EmvTestUi(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("继电器调试")
self.resize(1000, 600)
self.setStyleSheet("background-color: white;") # 背景颜色,根据主题来变化
self.__initWidget()
def __initWidget(self):
# 创建控件
self.__createWidget()
# 设置样式
self.__initStyles()
# 设置布局
self.__initLayout()
# 绑定
self.__bind()
def __createWidget(self):
# Tcp连接控件
# IP控件
self.ip_label = BodyLabel("设备IP: ")
self.ip_edit = LineEdit()
self.ip_edit.setText("192.168.0.18") # 默认IP
# 端口控件
self.port_label = BodyLabel("设备端口号: ")
self.port_edit = LineEdit()
self.port_edit.setText("50000") # 默认端口
# 连接按钮
self.connect_btn = PushButton("连接")
# 功能测试按钮
self.test_btn = PushButton("功能测试")
# 三个Do模块
self.do1_model = DOWidget("DO1")
self.do2_model = DOWidget("DO2")
self.do3_model = DOWidget("DO3")
def __initStyles(self):
# 设置样式 to do
pass
def __initLayout(self):
# 顶部的连接布局
top_layout = QHBoxLayout()
top_layout.addWidget(self.ip_label)
top_layout.addWidget(self.ip_edit)
top_layout.addWidget(self.port_label)
top_layout.addWidget(self.port_edit)
top_layout.addWidget(self.connect_btn)
top_layout.addWidget(self.test_btn)
# 三个Do开关区域布局
do_layout = QHBoxLayout()
do_layout.setSpacing(60) # DO模块之间的间距
do_layout.setAlignment(Qt.AlignCenter) # 水平居中
do_layout.addWidget(self.do1_model)
do_layout.addWidget(self.do2_model)
do_layout.addWidget(self.do3_model)
# 主布局
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(60, 60, 60, 60)
main_layout.setSpacing(30)
main_layout.addLayout(top_layout)
main_layout.addLayout(do_layout)
main_layout.addItem(
QSpacerItem(0, 0, QSizePolicy.Minimum, QSizePolicy.Expanding)
)
def __bind(self):
pass
self.connect_btn.clicked.connect(self.on_connect)
def on_connect(self):
"""连接按钮点击事件:切换状态为“连接中...”,模拟连接过程"""
# 禁用按钮 + 修改文字
self.connect_btn.setEnabled(False)
self.connect_btn.setText("连接中...")
# 模拟连接过程实际应替换为真实的网络请求如socket连接
self.timer = QTimer(self)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.simulate_connection_result)
self.timer.start(2000) # 模拟2秒连接耗时
def simulate_connection_result(self):
"""模拟连接结果(这里固定返回失败,实际需根据真实逻辑修改)"""
# 模拟连接失败实际中通过try-except或网络返回判断
connection_success = False # TODO: 替换为真实连接结果
if not connection_success:
# 弹出连接失败对话框
# 正确创建警告对话框:通过 type 参数指定为警告类型
msg_box = MessageBox(
"连接失败", # 标题
"无法连接到设备, 请检查IP和端口是否正确!!", # 内容
self, # 父窗口
)
msg_box.exec() # 显示对话框
# 恢复按钮状态
self.connect_btn.setEnabled(True)
self.connect_btn.setText("连接")
if __name__ == "__main__":
app = QApplication(sys.argv)
setTheme(Theme.LIGHT)
window = EmvTestUi()
window.show()
sys.exit(app.exec())

403
tests/test_login1.py Normal file
View File

@ -0,0 +1,403 @@
from common import *
import sys
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QMessageBox,
QFrame,
)
from PySide6.QtCore import Qt, QTimer
from PySide6.QtGui import QFont, QCursor
class LoginWindow(QMainWindow):
"""登录窗口类,优化宽高比例和布局美观度"""
def __init__(self, use_dark_theme=False):
super().__init__()
# 浅色主题样式表
self.light_style = """
QMainWindow, QWidget {
background-color: #f0f2f5;
}
QLabel#titleLabel {
font-weight: bold;
color: #1a73e8;
}
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 #1a73e8;
outline: none;
}
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-radius: 10px;
padding: 30px;
border: 1px solid #e0e0e0;
max-height: 350px;
max-width: 400px; /* 限制登录框最大宽度 */
}
"""
# 深色主题样式表
self.dark_style = """
QMainWindow, QWidget {
background-color: #1a1a1a;
}
QLabel#titleLabel {
font-weight: bold;
color: #8ab4f8;
}
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-radius: 10px;
padding: 30px;
border: 1px solid #444444;
max-height: 350px;
max-width: 400px; /* 限制登录框最大宽度 */
}
"""
# 初始化主题
if use_dark_theme:
self.setStyleSheet(self.dark_style)
else:
self.setStyleSheet(self.light_style)
self.init_ui()
def init_ui(self):
# 宽高比例调整为3:2宽度减少更紧凑美观
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)
# 登录框(宽度减少,更紧凑)
login_frame = QFrame()
login_frame.setObjectName("loginFrame")
login_layout = QVBoxLayout(login_frame)
login_layout.setSpacing(20)
login_layout.setContentsMargins(20, 20, 20, 20)
# 账号输入
self.username_input = QLineEdit()
self.username_input.setPlaceholderText("请输入账号")
self.username_input.setText("manager")
self.username_input.setMinimumWidth(250)
self.username_input.setMaximumWidth(350) # 限制输入框最大宽度
# 密码输入
self.password_input = QLineEdit()
self.password_input.setPlaceholderText("请输入密码")
self.password_input.setEchoMode(QLineEdit.Password)
self.password_input.setMinimumWidth(250)
self.password_input.setMaximumWidth(350)
# 登录按钮和忘记密码布局
btn_layout = QVBoxLayout()
self.login_btn = QPushButton("登录")
self.login_btn.clicked.connect(self.validate_login)
self.login_btn.setMinimumWidth(250)
self.login_btn.setMaximumWidth(350)
forgot_layout = QHBoxLayout()
forgot_layout.setAlignment(Qt.AlignLeft)
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()
btn_layout.addWidget(self.login_btn)
btn_layout.addLayout(forgot_layout)
login_layout.addWidget(self.username_input)
login_layout.addWidget(self.password_input)
login_layout.addLayout(btn_layout)
# 水平布局(登录框宽度占比减少,左右留白增加)
center_layout = QHBoxLayout()
center_layout.addStretch(2) # 左侧空白占2份比之前增加
center_layout.addWidget(login_frame, 3) # 登录框占3份比之前减少
center_layout.addStretch(2) # 右侧空白占2份比之前增加
main_layout.addLayout(center_layout, 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 // 66)) # 除数增大,字体增长更平缓
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)
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(use_dark_theme=False)
window.show()
sys.exit(app.exec())

508
tests/test_login2.py Normal file
View File

@ -0,0 +1,508 @@
from common import *
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())

189
tests/test_moju.py Normal file
View File

@ -0,0 +1,189 @@
from common import *
from PySide6.QtWidgets import (
QApplication,
QWidget,
QFormLayout,
QHBoxLayout,
QVBoxLayout,
QLineEdit,
QComboBox,
QPushButton,
QLabel,
QFileDialog,
QMessageBox,
)
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont
from qfluentwidgets import PushButton, ComboBox, LineEdit
# 模具管理界面
class MoldManagementUI(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# 初始化字体(用于左侧标签和控件)
self.left_font = QFont()
self.left_font.setPointSize(16) # 左侧标签字体大小
self.setWindowTitle("模具管理界面")
self.resize(1066, 630) # 窗口大小
self.__initWidget()
def __initWidget(self):
# 创建控件
self.__createWidgets()
# 设置样式
self.__initStyles()
# 布局
self.__initLayout()
# 绑定事件
self.__bind()
def __createWidgets(self):
# ========== 创建控件 ==========
# 左侧表单控件
self.serial_edit = LineEdit()
self.serial_edit.setPlaceholderText("模具序列号")
self.serial_edit.setFont(self.left_font) # 输入框字体
self.type_combo = ComboBox()
self.type_combo.addItems(["一板一个", "一板多个", "其他类型"])
self.type_combo.setFont(self.left_font) # 下拉框字体
# 左侧按钮(上传点位等)
self.btn_upload_model = PushButton("从文件上传")
self.btn_suction_point = PushButton("上传点位")
self.btn_place_point = PushButton("上传点位")
self.btn_pick_point = PushButton("上传点位")
self.btn_high_freq = PushButton("上传点位")
# 右侧操作按钮
self.btn_upload_mold = PushButton("上传模具")
self.btn_modify_mold = PushButton("修改模具")
self.btn_delete_mold = PushButton("删除模具")
self.btn_batch_upload = PushButton("通过文件上传") # 需要文件对话框
def __initStyles(self):
# ========== 设置样式 ==========
# 1. 左侧按钮样式(适中大小)
left_btn_style = """
QPushButton {
min-height: 40px;
min-width: 150px;
font-size: 14px;
padding: 5px 10px;
}
"""
for btn in [
self.btn_upload_model,
self.btn_suction_point,
self.btn_place_point,
self.btn_pick_point,
self.btn_high_freq,
]:
btn.setStyleSheet(left_btn_style)
# 2. 右侧按钮样式(较大尺寸)
right_btn_style = """
QPushButton {
min-height: 36px;
min-width: 166px;
font-size: 16px;
padding: 10px 20px;
}
"""
for btn in [
self.btn_upload_mold,
self.btn_modify_mold,
self.btn_delete_mold,
self.btn_batch_upload,
]:
btn.setStyleSheet(right_btn_style)
def __initLayout(self):
# ========== 布局设计 =========
# 左侧表单布局(创建标签并设置字体)
form_layout = QFormLayout()
form_layout.setSpacing(20) # 行间距
form_layout.setRowWrapPolicy(QFormLayout.DontWrapRows)
# 添加表单行(左侧标签+控件)
label_config = [
("模具序列号:", self.serial_edit),
("模具类型:", self.type_combo),
("模具三维模型:", self.btn_upload_model),
("吸取点位设置:", self.btn_suction_point),
("放置点位设置:", self.btn_place_point),
("取料点位设置:", self.btn_pick_point),
("高周波放料点位设置:", self.btn_high_freq),
]
for text, widget in label_config:
label = QLabel(text)
label.setFont(self.left_font)
form_layout.addRow(label, widget)
# 右侧垂直布局(按钮更大,保持居中)
right_layout = QVBoxLayout()
right_layout.addWidget(self.btn_upload_mold)
right_layout.addWidget(self.btn_modify_mold)
right_layout.addWidget(self.btn_delete_mold)
right_layout.addWidget(self.btn_batch_upload)
right_layout.setAlignment(Qt.AlignCenter)
right_layout.setSpacing(66) # 右侧按钮间距
# 主布局左侧2/3右侧1/3
main_layout = QHBoxLayout()
main_layout.addLayout(form_layout)
main_layout.addLayout(right_layout)
main_layout.setContentsMargins(50, 50, 50, 50)
main_layout.setSpacing(66)
main_layout.setStretchFactor(form_layout, 2)
main_layout.setStretchFactor(right_layout, 1)
self.setLayout(main_layout)
def __bind(self):
# ========== 绑定事件 ==========
self.btn_batch_upload.clicked.connect(self.onFileUpload) # 文件上传
self.btn_upload_model.clicked.connect(self.on3DModelUpload) # 三维模型上传
# 文件上传 (暂时不知道是上传什么文件,待定)
def onFileUpload(self):
"""打开文件对话框"""
# 打开文件选择框
file_paths, _ = QFileDialog.getOpenFileNames(
self, "选择上传文件", "", "所有文件 (*)"
)
if file_paths:
# 显示选中的文件数量(实际应用中可替换为上传逻辑)
QMessageBox.information(self, "文件选择完成", f"准备上传!")
# 这里可以添加文件上传的业务逻辑
print("选中的文件路径:", file_paths)
# 三维模型上传
def on3DModelUpload(self):
"""打开三维模型文件对话框"""
# 只允许选择三维模型相关格式
file_path, _ = QFileDialog.getOpenFileName(
self, "选择三维模型文件", "", "模型文件 (*.stl *.obj *.step);;所有文件 (*)"
)
if file_path:
QMessageBox.information(
self, "模型选择完成", f"已选择模型文件:\n{file_path}"
)
# 这里可以添加模型上传的业务逻辑
print("选中的模型路径:", file_path)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = MoldManagementUI()
window.show()
sys.exit(app.exec())

107
tests/test_register.py Normal file
View File

@ -0,0 +1,107 @@
from common import *
from PySide6.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QFormLayout,
QLineEdit,
QComboBox,
QPushButton,
QLabel,
QSpacerItem,
QSizePolicy,
)
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont
from qfluentwidgets import PushButton, ComboBox, LineEdit
class RegistrationUI(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.init_ui()
def init_ui(self):
# 窗口基础设置
self.setWindowTitle("用户注册")
self.resize(800, 600) # 窗口大小
self.setStyleSheet("background-color: #f0f2f5;") # 浅灰背景
# ========== 注册表单(居中显示) ==========
# 表单容器(带白色背景和阴影,增强层次感)
form_container = QWidget()
form_container.setStyleSheet(
"""
QWidget {
background-color: white;
border-radius: 10px;
}
"""
)
form_container.setFixedWidth(466) # 表单固定宽度
# 表单标题
title_label = QLabel("Register")
title_label.setFont(QFont("Microsoft YaHei", 18, QFont.Bold))
title_label.setAlignment(Qt.AlignCenter)
title_label.setStyleSheet("margin: 20px 0; color: #333;")
# 注册控件
self.account_edit = LineEdit()
self.account_edit.setPlaceholderText("请输入账号")
self.password_edit = LineEdit()
self.password_edit.setEchoMode(QLineEdit.Password)
self.password_edit.setPlaceholderText("请输入密码")
self.confirm_edit = LineEdit()
self.confirm_edit.setEchoMode(QLineEdit.Password)
self.confirm_edit.setPlaceholderText("请确认密码")
# 权限选择(调试人员 + 车间主管)
self.permission_combo = ComboBox()
self.permission_combo.addItems(["调试人员", "车间主管"])
# 确认按钮(红色样式)
self.confirm_btn = PushButton("确认")
self.confirm_btn.setCursor(Qt.PointingHandCursor)
# 表单布局QFormLayout 自动对齐标签和输入框)
form_layout = QFormLayout()
form_layout.addRow("账号:", self.account_edit)
form_layout.addRow("密码:", self.password_edit)
form_layout.addRow("确认密码:", self.confirm_edit)
form_layout.addRow("权限:", self.permission_combo)
form_layout.setVerticalSpacing(16) # 行间距
form_layout.setLabelAlignment(Qt.AlignRight | Qt.AlignVCenter) # 标签右对齐
# 表单容器内部布局(垂直排列标题、表单、按钮)
container_layout = QVBoxLayout(form_container)
container_layout.addWidget(title_label)
container_layout.addLayout(form_layout)
container_layout.addWidget(self.confirm_btn, alignment=Qt.AlignCenter)
container_layout.setContentsMargins(30, 10, 30, 30) # 容器内边距
container_layout.setSpacing(20) # 控件间距
# ========== 主布局(让表单居中显示) ==========
main_layout = QVBoxLayout(self)
# 顶部和底部添加弹簧,实现垂直居中
main_layout.addItem(
QSpacerItem(0, 0, QSizePolicy.Minimum, QSizePolicy.Expanding)
)
main_layout.addWidget(form_container, alignment=Qt.AlignCenter) # 表单水平居中
main_layout.addItem(
QSpacerItem(0, 0, QSizePolicy.Minimum, QSizePolicy.Expanding)
)
# 左右留空(可选,控制表单左右边距)
main_layout.setContentsMargins(50, 50, 50, 50)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = RegistrationUI()
window.show()
sys.exit(app.exec())

98
tests/test_teachhtml.py Normal file
View File

@ -0,0 +1,98 @@
from common import *
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtCore import QUrl, QTimer
# 示教器页面
class TeachBrowser(QWidget):
def __init__(
self,
parent=None,
url="http://192.168.58.2/login.html",
username="admin",
password="123",
):
super().__init__(parent=parent)
self.setWindowTitle("示教器页面")
# self.setGeometry(100, 100, 1024, 768)
# 1. 配置登录信息
self.login_username = username # 登录的用户名
self.login_password = password # 登录的密码
# 2. 初始化浏览器视图
self.web_view = QWebEngineView()
# 加载登录页面
self.web_view.load(QUrl(url))
# 3. 绑定网页加载完成信号
self.web_view.loadFinished.connect(self.onPageLoaded)
# 4. 设置窗口布局
layout = QVBoxLayout(self)
layout.addWidget(self.web_view)
def onPageLoaded(self, success):
"""页面加载完成后,执行登录逻辑"""
if not success:
# print("登录页面加载失败!")
return
self.injectLoginJs()
self.web_view.loadFinished.disconnect(self.onPageLoaded)
def injectLoginJs(self):
"""注入 JavaScript 代码,自动输入用户名和密码"""
auto_login_js = f"""
// 1. 等待 AngularJS 完全初始化(确保能获取到 scope
function waitForAngular() {{
return new Promise((resolve) => {{
const checkAngular = () => {{
// 判断 Angular 是否加载完成且登录表单if_Login0已显示
if (window.angular && document.querySelector('#loginForm')) {{
resolve();
}} else {{
setTimeout(checkAngular, 200); // 每 200ms 检查一次
}}
}};
checkAngular();
}});
}}
// 2. 核心登录逻辑
async function autoLogin() {{
await waitForAngular(); // 等待 Angular 初始化
// 获取第一个登录表单id="loginForm"ng-if="if_Login0")的 Angular 作用域
const loginForm = document.querySelector('#loginForm');
const scope = window.angular.element(loginForm).scope();
if (!scope) {{
return;
}}
// 3. 填充用户名和密码(对应 ng-model="$parent.userName""$parent.password"
scope.$apply(() => {{
scope.$parent.userName = "{self.login_username}"; // 用户名
scope.$parent.password = "{self.login_password}"; // 密码
}});
}}
// 执行登录
autoLogin();
"""
# 执行注入的 JS 代码
self.web_view.page().runJavaScript(auto_login_js)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TeachBrowser()
window.show()
sys.exit(app.exec())

146
tests/test_weight.py Normal file
View File

@ -0,0 +1,146 @@
from common import *
from PySide6.QtWidgets import (
QApplication,
QWidget,
QFormLayout,
QHBoxLayout,
QVBoxLayout,
QLabel,
QSpacerItem,
QSizePolicy,
)
from PySide6.QtCore import Qt
from qfluentwidgets import LineEdit, PushButton, StrongBodyLabel
class WeightControlUI(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# ========== 1. 窗口基础设置 ==========
self.setWindowTitle("计量一体机调试")
self.resize(800, 600) # 窗口大小
self.setStyleSheet("background-color: white;") # 背景颜色,根据主题来变化
self.__initWidget()
def __initWidget(self):
# 创建控件
self.__createWidget()
# 设置样式
self.__initStyles()
# 设置布局
self.__initLayout()
def __createWidget(self):
# ========== 创建表单控件 ==========
# 右侧编辑栏
self.target_weight_edit = LineEdit()
self.target_weight_edit.setPlaceholderText("目标重量")
self.current_weight_edit = LineEdit()
self.current_weight_edit.setPlaceholderText("当前重量")
self.current_weight_edit.setReadOnly(True) # 只读
self.error_edit = LineEdit()
self.error_edit.setPlaceholderText("误差")
self.error_edit.setReadOnly(True) # 只读
# ========== 按钮 ==========
self.calibrate_btn = PushButton("重量标定")
self.confirm_btn = PushButton("确认")
self.compensate_btn = PushButton("补称")
self.motor_forward_btn = PushButton("电机正转")
self.motor_reverse_btn = PushButton("电机反转")
def __initStyles(self):
# 设置按钮大小
# 固定大小为 160x46
for btn in [
self.calibrate_btn,
self.confirm_btn,
self.compensate_btn,
self.motor_forward_btn,
self.motor_reverse_btn,
]:
btn.setFixedSize(160, 46) # 固定按钮尺寸
def __initLayout(self):
# ========== 布局设计 ==========
# 表单布局(标签+输入框,右对齐)
form_layout = QFormLayout()
form_layout.addRow(StrongBodyLabel("目标重量:"), self.target_weight_edit)
form_layout.addRow(StrongBodyLabel("当前重量:"), self.current_weight_edit)
form_layout.addRow(StrongBodyLabel("误差:"), self.error_edit)
form_layout.setRowWrapPolicy(QFormLayout.DontWrapRows) # 禁止换行
form_layout.setVerticalSpacing(20) # 行间距
form_layout.setLabelAlignment(Qt.AlignLeft | Qt.AlignVCenter) # 标签左对齐
# 用QHBoxLayout 包裹 form_layout两侧加弹簧
form_horizontal_layout = QHBoxLayout()
# 左侧弹簧:占据左边多余空间
form_horizontal_layout.addItem(
QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum)
)
# 添加表单布局
form_horizontal_layout.addLayout(form_layout)
# 右侧弹簧:占据右边多余空间
form_horizontal_layout.addItem(
QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum)
)
# 按钮布局(第一行:重量标定 + 确认;第二行:补称 + 电机正转 + 电机反转)
# 第一行按钮布局(中间留空,让按钮居中)
btn_row1_layout = QHBoxLayout()
btn_row1_layout.addSpacerItem(
QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum)
)
btn_row1_layout.addWidget(self.calibrate_btn)
btn_row1_layout.addSpacerItem(
QSpacerItem(160, 0, QSizePolicy.Fixed, QSizePolicy.Minimum)
) # 两个按钮之间的间距为160
btn_row1_layout.addWidget(self.confirm_btn)
btn_row1_layout.addSpacerItem(
QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Minimum)
)
# 第二行按钮布局(均匀分布)
btn_row2_layout = QHBoxLayout()
btn_row2_layout.addWidget(self.compensate_btn)
btn_row2_layout.addSpacerItem(
QSpacerItem(60, 0, QSizePolicy.Fixed, QSizePolicy.Minimum)
)
btn_row2_layout.addWidget(self.motor_forward_btn)
btn_row2_layout.addSpacerItem(
QSpacerItem(60, 0, QSizePolicy.Fixed, QSizePolicy.Minimum)
)
btn_row2_layout.addWidget(self.motor_reverse_btn)
btn_row2_layout.setContentsMargins(0, 0, 0, 0) # 清除边距
# 主布局(垂直排列:表单 + 按钮行1 + 按钮行2
main_layout = QVBoxLayout(self)
main_layout.addLayout(form_horizontal_layout)
main_layout.addSpacing(40) # 表单与按钮的间距
main_layout.addLayout(btn_row1_layout)
main_layout.addSpacing(30) # 两行按钮的间距
main_layout.addLayout(btn_row2_layout)
main_layout.setContentsMargins(80, 60, 80, 60) # 窗口内边距
# main_layout.setAlignment(Qt.AlignTop | Qt.AlignHCenter) # 整体上对齐,水平居中
main_layout.setAlignment(Qt.AlignVCenter | Qt.AlignHCenter) # 垂直、水平居中
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
# 设置qfluentwidgets全局主题如浅色主题
from qfluentwidgets import setTheme, Theme
setTheme(Theme.LIGHT)
window = WeightControlUI()
window.show()
sys.exit(app.exec())