【wxpython】启动弹窗

Friday, July 21, 2023
本文共497字
1分钟阅读时长

⚠️本文是作者P3troL1er原创,首发于https://peterliuzhi.top/posts/wxpython%E5%90%AF%E5%8A%A8%E5%BC%B9%E7%AA%97/。商业转载请联系作者获得授权,非商业转载请注明出处!

The least movement is of importance to all nature. The entire ocean is affected by a pebble. — Blaise Pascal

继承wx.Dialog进行布局,用户点击确定的时候更改自己的成员变量:

class LoginDialog(wx.Dialog):
    def __init__(self, parent=None, compulsory_login_flag=True):
        super(LoginDialog, self).__init__(parent, -1, title='登录SQLserver',
                                          style=wx.DEFAULT_DIALOG_STYLE)
        self._compulsory_login_flag = compulsory_login_flag

    def InitUI(self, default_host, default_user, default_db, default_port):
        self.SetSize((250, 300))
        # 操作组件
        self.input_host = wx.TextCtrl(self, value=str(default_host))
        self.input_name = wx.TextCtrl(self, value=str(default_user))
        self.input_pass = wx.TextCtrl(self, style=wx.TE_PASSWORD)
        self.input_db = wx.TextCtrl(self, value=str(default_db))
        self.input_port = wx.TextCtrl(self, value=str(default_port))
        self.btn_1a = wx.Button(self, label='登录', size=(7, 35), id=wx.ID_OK)
        # 装饰组件
        label_host = wx.StaticText(self, label='服务器:')
        label_name = wx.StaticText(self, label='用户名:')
        label_pass = wx.StaticText(self, label='密码')
        label_db = wx.StaticText(self, label='初始数据库:')
        label_port = wx.StaticText(self, label='端口')
        # panel_black = wx.Panel(self, size=(7, 15))
        # panel_black.SetBackgroundColour(wx.Colour('#474747'))
        # panel_red = wx.Panel(self, size=(7, 30))
        # panel_red.SetBackgroundColour(wx.Colour('#bf0006'))
        # 布局组件
        fgs_2 = wx.FlexGridSizer(rows=5, cols=2, vgap=8, hgap=10)
        fgs_2.AddMany([
            (label_host, 1, wx.LEFT, 5), (self.input_host, 1, wx.EXPAND),
            (label_port, 1, wx.LEFT, 5), (self.input_port, 1, wx.EXPAND),
            (label_name, 1, wx.LEFT, 5), (self.input_name, 1, wx.EXPAND),
            (label_pass, 1, wx.LEFT, 5), (self.input_pass, 1, wx.EXPAND),
            (label_db, 1, wx.LEFT, 5), (self.input_db, 1, wx.EXPAND),
        ])
        fgs_2.AddGrowableCol(1)
        fgs_1 = wx.FlexGridSizer(rows=2, cols=1, vgap=0, hgap=0)
        fgs_1.AddMany([
            (fgs_2, 1, wx.EXPAND | wx.TOP | wx.CENTER | wx.RIGHT | wx.LEFT, 15),
            (self.btn_1a, 1, wx.EXPAND | wx.ALL, 15),
        ])
        fgs_1.AddGrowableCol(0)
        fgs_1.AddGrowableRow(0, 2)
        fgs_1.AddGrowableRow(1, 2)
        # fgs_1.AddGrowableRow(3, 1)
        # 窗口组件
        self.SetSizer(fgs_1)
        self.Center()
        self.Bind(wx.EVT_CLOSE, self._justExit)
        self.Bind(wx.EVT_BUTTON, self._handle_before_close_dialog, id=wx.ID_OK)

    def _handle_before_close_dialog(self, e):
        if self._compulsory_login_flag:
            wx.Exit()
        else:
            self.loginInfo = {
                'userName': self.input_name.GetValue(),
                'serverName': self.input_host.GetValue(),
                'dbName': self.input_db.GetValue(),
                'port': self.input_port.GetValue(),
                'passWord': self.input_pass.GetValue()
            }
            self.Destroy()
    def _justExit(self, event):
        self.loginInfo = False
        self.Destroy()

使用的时候创建对象然后调用完获取其成员变量就好:

login = LoginDialog(compulsory_login_flag=False)
login.InitUI(self.serverName, self.userName, self.dbName, self.port) # 初始化UI
login.ShowModal() # wx.Dialog内置函数
loginInfo = login.loginInfo

整个过程很流畅,很优雅,但是我一开始没有查到(x)