如何获取wxPython自定义Dialog的数据?

发布于 2022-09-06 00:42:18 字数 4312 浏览 27 评论 0

wxPython的许多例子都介绍了wx自带的对话框,许多都是单一控件或者系统自定义控件,但是用户自定义控件如何获取数据都没有提供。

例如,我要设置串口,必然在一个自定义对话框中收集串口名称、波特率、数据宽度、校验方式、校验位等,或采用Choice,或采用TextCtrl来获取。那么这些数据如何传递给Frame对象呢?

难道是通过全局对象来传递?

import wx

class SerialConfDialog(wx.Dialog):
    def __init__(self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, 
            style=wx.DEFAULT_DIALOG_STYLE, ports=[]):
        pre = wx.PreDialog()
        pre.Create(parent, ID, title, pos, size, style)
        self.PostCreate(pre)
        sizer = wx.BoxSizer(wx.VERTICAL)

        label = wx.StaticText(self, -1, "UART/BLE/USB VCP settings")
        sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Serial:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        port = wx.Choice(self, -1, (100,50), choices=ports)
        if len(ports):
            port.SetSelection(0)
        box.Add(port, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        bauds = ['300','600','1200','2400','4800','7200','9600',\
            '14400','19200','28800','38400','43000','56000',\
            '57600','115200','230400']

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Baud:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        baud = wx.Choice(self, -1, (100,50), choices=bauds)
        baud.SetSelection(6)
        box.Add(baud, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        widths = ['5','6','7','8']

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Width:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        width = wx.Choice(self, -1, (100,50), choices=widths)
        width.SetSelection(3)
        box.Add(width, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        checks = ['None','Even','Odd','Mark','Space']
        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "CRC:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        check = wx.Choice(self, -1, (100,50), choices=checks)
        check.SetSelection(0)
        box.Add(check, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        stops = ['1','1.5','2']
        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Stop:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        stop = wx.Choice(self, -1, (100,50), choices=stops)
        stop.SetSelection(0)
        box.Add(stop, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

        btnsizer = wx.StdDialogButtonSizer()
        
        if wx.Platform != "__WXMSW__":
            btn = wx.ContextHelpButton(self)
            btnsizer.AddButton(btn)
        
        btn = wx.Button(self, wx.ID_OK)
        #btn.SetHelpText("The OK button completes the dialog")
        btn.SetDefault()
        btnsizer.AddButton(btn)

        btn = wx.Button(self, wx.ID_CANCEL)
        #btn.SetHelpText("The Cancel button cancels the dialog. (Cool, huh?)")
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        self.SetSizer(sizer)
        sizer.Fit(self)
        
class ToolbarFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "Radio Terminal",
            size=(450,500))
        panel = wx.Panel(self)
        panel.SetBackgroundColour("White")
        
        # Menu init code here.....
        self.Bind(wx.EVT_MENU, self.OnSerialConf, miSerial)
        
    def OnSerialConf(self, event):
        dlg = SerialConfDialog(self, -1, "Serial Configurations", \
            size=(350,200), style=wx.DEFAULT_DIALOG_STYLE, ports=self.ports)
        val = dlg.ShowModal()
    
        if val == wx.ID_OK:
            print "You pressed OK"
        else:
            print "You pressed Cancel"

        dlg.Destroy()    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我已经知道答案了。解决方法是在Dialog的__init__()中将所有选项对象标注为类的私有对象,然后写个方法返回所有数值,同时可以通过OK/Cancel来确定返回值是否有效。

如果有更好的方式,请继续。

class SerialConfDialog(wx.Dialog):
    def __init__(self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, 
            style=wx.DEFAULT_DIALOG_STYLE, ports=[]):
        pre = wx.PreDialog()
        #pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
        pre.Create(parent, ID, title, pos, size, style)
        self.PostCreate(pre)
        sizer = wx.BoxSizer(wx.VERTICAL)

        label = wx.StaticText(self, -1, "UART/BLE/USB VCP settings")
        #label.SetHelpText("This is the help text for the label")
        sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Serial:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        #text = wx.TextCtrl(self, -1, "", size=(80,-1))
        self.port = wx.Choice(self, -1, (100,50), choices=ports)
        if len(ports):
            self.port.SetSelection(0)
        box.Add(self.port, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        bauds = ['300','600','1200','2400','4800','7200','9600',\
            '14400','19200','28800','38400','43000','56000',\
            '57600','115200','230400']

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Baud:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        self.baud = wx.Choice(self, -1, (100,50), choices=bauds)
        self.baud.SetSelection(6)
        box.Add(self.baud, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        widths = ['5','6','7','8']

        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Width:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        self.width = wx.Choice(self, -1, (100,50), choices=widths)
        self.width.SetSelection(3)
        box.Add(self.width, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        checks = ['None','Even','Odd','Mark','Space']
        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "CRC:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        self.parity = wx.Choice(self, -1, (100,50), choices=checks)
        self.parity.SetSelection(0)
        box.Add(self.parity, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        stops = ['1','1.5','2']
        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Stop:")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        self.stop = wx.Choice(self, -1, (100,50), choices=stops)
        self.stop.SetSelection(0)
        box.Add(self.stop, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)


        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

        btnsizer = wx.StdDialogButtonSizer()
        
        if wx.Platform != "__WXMSW__":
            btn = wx.ContextHelpButton(self)
            btnsizer.AddButton(btn)
        
        btn = wx.Button(self, wx.ID_OK)
        #btn.SetHelpText("The OK button completes the dialog")
        btn.SetDefault()
        btnsizer.AddButton(btn)

        btn = wx.Button(self, wx.ID_CANCEL)
        #btn.SetHelpText("The Cancel button cancels the dialog. (Cool, huh?)")
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        self.SetSizer(sizer)
        sizer.Fit(self)

    def GetSelections(self):
        return (self.port.GetSelection(), self.baud.GetSelection(), \
            self.width.GetSelection(), self.parity.GetSelection(), self.stop.GetSelection())
兰花执着 2022-09-13 00:42:18

myDialog.xxx.getValue()
这样就能获得了,先加个Ok按钮,self.ret=0,点击这个按钮时返回self.ret=1,然后判断myDialog.ret==1就取所有需要的值,反之则不取,关闭窗口也不取值。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文