#coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1,"Wxpython Radiobox 演示",size = (800,600)) panel = wx.Panel(self) #第一種方法使用wx.RadioButton類 #RadioButton(parent, id=ID_ANY, label=EmptyString, # pos=DefaultPosition, size=DefaultSize, style=0, validator=DefaultValidator, name=RadioButtonNameStr) self.check1 = wx.RadioButton(panel,-1,"Value1",pos = (50,20),style = wx.RB_GROUP) self.check2 = wx.RadioButton(panel, -1, "Value2", pos=(150,20)) self.check3 = wx.RadioButton(panel, -1, "Value3", pos=(250,20)) self.check1.Bind(wx.EVT_RADIOBUTTON,self.Event1) self.check2.Bind(wx.EVT_RADIOBUTTON,self.Event1) self.check3.Bind(wx.EVT_RADIOBUTTON,self.Event1) self.check4 = wx.RadioButton(panel, -1, "Value2-1", pos=(50,60), style=wx.RB_GROUP) self.check5 = wx.RadioButton(panel, -1, "Value2-2", pos=(150,60)) self.check6 = wx.RadioButton(panel, -1, "Value2-3", pos=(250,60)) self.check4.Bind(wx.EVT_RADIOBUTTON,self.Event2) self.check5.Bind(wx.EVT_RADIOBUTTON, self.Event2) self.check6.Bind(wx.EVT_RADIOBUTTON, self.Event2) #第二種方法使用wx.RadioBox類 list3 = ["Value3-1","Value3-2" ,"Value3-3"] #RadioBox(parent, id=ID_ANY, label=EmptyString, pos=DefaultPosition, size=DefaultSize, # choices=[], majorDimension=0, style=RA_SPECIFY_COLS, validator=DefaultValidator, name=RadioBoxNameStr) self.radiobox3 = wx.RadioBox(panel,-1,"測試3",pos=(50,100),choices=list3,style=wx.RA_SPECIFY_COLS) self.radiobox3.Bind(wx.EVT_RADIOBOX,self.Event3) list4 = ["Value4-1","Value4-2" ,"Value4-3"] self.radiobox4 = wx.RadioBox(panel, -1, "測試4",pos=(50, 180),choices=list4, style=wx.RA_SPECIFY_ROWS) self.radiobox4.Bind(wx.EVT_RADIOBOX, self.Event4) #設定初始值,按序號進行選擇,序號從0開始計數 self.radiobox3.SetSelection(2) #方式2,設定初始值,經過文本進行選擇 self.radiobox4.SetSelection(self.radiobox4.FindString("Value4-3")) def Event1(self,event): print "測試1:",self.check1.GetLabel() def Event2(self,event): print "測試2:", self.check4.GetLabel() def Event3(self,event): #方式1獲取選中項 print "測試3:",self.radiobox3.GetStringSelection(),self.radiobox3.GetSelection() #方式2獲取選中項,更靈活 print "測試3.1",event.GetString(),event.GetInt() def Event4(self,event): print "測試4:",self.radiobox4.GetStringSelection() if __name__ == "__main__": app = wx.App() frame = MyFrame() frame.Show() app.MainLoop()
不排版了,直接放代碼,乾貨以下:python
一、示範了兩種寫法,方法二更簡單。app
二、示範瞭如何在點擊事件中獲取 選中項oop
三、示範如何設置初始值,尤爲是根據文本選中,折磨了我好長時間,在源碼裏猜出來的。測試