如何获取wxPython自定义Dialog的数据?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经知道答案了。解决方法是在Dialog的__init__()中将所有选项对象标注为类的私有对象,然后写个方法返回所有数值,同时可以通过OK/Cancel来确定返回值是否有效。
如果有更好的方式,请继续。
myDialog.xxx.getValue()
这样就能获得了,先加个Ok按钮,self.ret=0,点击这个按钮时返回self.ret=1,然后判断myDialog.ret==1就取所有需要的值,反之则不取,关闭窗口也不取值。