Files
fluent_widgets_pyside6/tests/test_emv.py

235 lines
6.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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())