from PyQt5.QtWidgets import QDialog, QFormLayout, QLineEdit, QComboBox, QDialogButtonBox, QMessageBox from PyQt5.QtGui import QPixmap, QIcon from PyQt5.QtCore import Qt import sys import re class DeviceDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.initUI() def initUI(self): layout = QFormLayout() proType = QComboBox() proType.addItems(['DP', 'PA']) proType.setObjectName('ProtocolType') masterSlaveModel = QComboBox() masterSlaveModel.addItems(['主站', '从站']) masterSlaveModel.setObjectName('masterSlaveModel') deviceName = QLineEdit() deviceName.setObjectName('deviceName') pvUpperLimit = QLineEdit() pvUpperLimit.setObjectName('pvUpperLimit') pvLowerLimit = QLineEdit() pvLowerLimit.setObjectName('pvLowerLimit') pvUnit = QLineEdit() pvUnit.setObjectName('pvUnit') layout.addRow('协议类型', proType) layout.addRow('主从模式', masterSlaveModel) layout.addRow("设备名:", deviceName) layout.addRow("量程上限:", pvUpperLimit) layout.addRow("量程下限:", pvLowerLimit) layout.addRow("单位:", pvUnit) button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) ok_button = button_box.button(QDialogButtonBox.Ok) ok_button.setText("确定") # 设置Ok按钮的文本 cancel_button = button_box.button(QDialogButtonBox.Cancel) cancel_button.setText("取消") # 设置Cancel按钮的文本 button_box.accepted.connect(self.check_input) button_box.rejected.connect(self.reject) layout.addRow(button_box) self.setWindowIcon(QIcon('../Static/zhjt.ico')) self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint) # 去掉标题栏的问号 self.setLayout(layout) self.setWindowTitle("设备信息") def getParameters(self): proType = self.findChild(QComboBox, "ProtocolType").currentText() masterSlaveModel = self.findChild(QComboBox, "masterSlaveModel").currentText() deviceName = self.findChild(QLineEdit, "deviceName").text() pvUpperLimit = self.findChild(QLineEdit, "pvUpperLimit").text() pvLowerLimit = self.findChild(QLineEdit, "pvLowerLimit").text() pvUnit = self.findChild(QLineEdit, "pvUnit").text() return deviceName, proType, masterSlaveModel, pvUpperLimit, pvLowerLimit, pvUnit def check_input(self): deviceName, proType, masterSlaveModel, pvUpperLimit, pvLowerLimit, pvUnit = self.getParameters() if not pvUpperLimit or not pvLowerLimit or not pvUnit or not deviceName: QMessageBox.warning(self, '警告', '有值未输入。') elif not re.match(r'^[-+]?\d*\.?\d*$', pvUpperLimit) or not re.match(r'^[-+]?\d*\.?\d*$', pvLowerLimit): QMessageBox.warning(self, '警告', '请输入数字。') elif pvUpperLimit < pvLowerLimit: QMessageBox.warning(self, '警告', '量程输入有误。') else: self.accept() # 所有输入都是数字且不为空时接受对话框