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()