PyQt——dialog自定义对话框

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QDialog, QLineEdit, QLabel, QPushButton, QMessageBox, QGridLayout
from PyQt5.QtCore import Qt, QEvent, QRegExp
from PyQt5.QtGui import QKeyEvent, QKeySequence, QRegExpValidator
import qtawesome
from core.tools.setting_tool import SettingTool

class TelecommunicationSettingsDialog(QDialog):
    setting_tool = SettingTool()
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle("网络通信设置")  # 窗口标题
        self.setWindowIcon(qtawesome.icon('fa.rss-square', color='gray'))
        #--------------------加个logal----------------------------
        self.setGeometry(400, 400, 250, 300)  # 窗口位置与大小
        self.setFixedSize(300, 300)

        self.glayout = QGridLayout()

        self.ip_address_label = QLabel('IP地址:')
        self.ip_address_label.setObjectName("ip_address_label")
        self.glayout.addWidget(self.ip_address_label, 0, 0)
        self.port_label = QLabel('端口号:')
        self.port_label.setObjectName("port_label")
        self.glayout.addWidget(self.port_label, 1, 0)

        self.ip_address_edit = QLineEdit()  # 用于接收用户输入的IP地址
        self.ip_address_edit.setObjectName("ip_address_edit")
        # self.ip_address_edit.setContextMenuPolicy(Qt.NoContextMenu)
        self.ip_address_edit.setPlaceholderText("192.168.0.1")
        # self.ip_address_edit.setEchoMode(QLineEdit.Password)
        # regx = QRegExp("^[a-zA-Z][0-9A-Za-z]{14}$")
        # validator = QRegExpValidator(regx, self.ip_address_edit)
        # self.ip_address_edit.setValidator(validator)

        self.ip_address_edit.installEventFilter(self)
        self.glayout.addWidget(self.ip_address_edit, 0, 1)

        self.port_edit = QLineEdit()   # 用于接收用户输入的端口号
        self.port_edit.setObjectName("port_edit")
        self.port_edit.setPlaceholderText("9000")
        self.port_edit.installEventFilter(self)
        self.glayout.addWidget(self.port_edit, 1, 1)

        self.buttons = QtWidgets.QDialogButtonBox(
            QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)  # 窗口中建立确认和取消按钮
        self.glayout.addWidget(self.buttons, 2, 1)

        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)

        self.setLayout(self.glayout)

    def get_data(self):  # 定义获取用户输入数据的方法
        return self.ip_address_edit.text(), self.port_edit.text()

    def set_data(self):
        self.setting_tool.save_one_setting("UpMachine/setting/network_ip_address", self.ip_address_edit.text())
        self.setting_tool.save_one_setting("UpMachine/setting/network_port", self.port_edit.text())
        print("网络通信设置设置成功")
        return True


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    v = TelecommunicationSettingsDialog()  # 建立对话框实例
    if v.exec_(): # 执行方法,成为模态对话框,用户点击OK后,返回1
 		name,game = v.get_data()
    sys.exit(app.exec_())


你可能感兴趣的:(PyQt)