from PyQt5.QtWidgets import QDialog, QFormLayout, QLineEdit, QComboBox, QDialogButtonBox, QApplication, QMessageBox from PyQt5.QtGui import QPixmap, QIcon from PyQt5.QtCore import Qt import sys import re class AreaSettingWidget(QDialog): def __init__(self, parent=None): super().__init__(parent) self.initUI() def initUI(self): layout = QFormLayout() varType = QComboBox() varType.addItems(['AI', 'AO', 'DI', 'DO']) varType.setObjectName('varType') channelNums = QLineEdit() channelNums.setObjectName('channelNums') channelBytes = QLineEdit() channelBytes.setObjectName('channelBytes') layout.addRow('协议类型:', varType) layout.addRow('通道数:', channelNums) layout.addRow("字节长度:", channelBytes) 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): varType = self.findChild(QComboBox, "varType").currentText() channelNums = self.findChild(QLineEdit, "channelNums").text() channelBytes = self.findChild(QLineEdit, "channelBytes").text() return varType, channelNums, channelBytes def check_input(self): varType, channelNums, channelBytes = self.getParameters() if not channelNums or not channelBytes: QMessageBox.warning(self, '警告', '有值未输入。') elif not re.match(r'^[-+]?\d*\.?\d*$', channelNums) or not re.match(r'^[-+]?\d*\.?\d*$', channelBytes): QMessageBox.warning(self, '警告', '请输入数字。') else: self.accept() # 所有输入都是数字且不为空时接受对话框 if __name__ == '__main__': app = QApplication(sys.argv) window = AreaSettingWidget() window.show() sys.exit(app.exec_())