wxPython中listbox用法实例详解
本文实例讲述了wxPython中listbox用法。分享给大家供大家参考。具体如下:
#loadalistboxwithnames,selectanameanddisplayintitle #experimentswithwxPythonbyvegaseat20mar2005 #Pythonv2.4andwxPythonv2.5 #Ifyouhavenotalreadydoneso,installPython2.4first. #Iusedpython-2.4.1c2.msi(thisistheself-extracting #MS-Installerfile)fromhttp://www.python.org #TheninstallwxPython2.5-win32-unicode-2.5.4.1-py24.exe #from:http://prdownloads.sourceforge.net/wxpython/ #(ifyoudon'tgetintounicode,downloadtheansiversion) #note:python-2.4.1c2.msishouldsoonbepython-2.4.1.msi importwx defcreate(parent): returnFrame1(parent) #assignIDnumbers [wxID_FRAME1,wxID_FRAME1BUTTON1,wxID_FRAME1BUTTON2,wxID_FRAME1LISTBOX1, ]=[wx.NewId()for_init_ctrlsinrange(4)] classFrame1(wx.Frame): def_init_ctrls(self,prnt): #BOAgeneratedmethods wx.Frame.__init__(self,id=wxID_FRAME1,name='',parent=prnt, pos=wx.Point(358,184),size=wx.Size(299,387), style=wx.DEFAULT_FRAME_STYLE,title=u'ListBoxTest...') self.SetClientSize(wx.Size(291,347)) self.SetBackgroundColour(wx.Colour(0,128,0)) self.button1=wx.Button(id=wxID_FRAME1BUTTON1,label=u'LoadListBox', name='button1',parent=self,pos=wx.Point(8,8),size=wx.Size(176, 28),style=0) self.button1.Bind(wx.EVT_BUTTON,self.OnButton1Button, id=wxID_FRAME1BUTTON1) self.listBox1=wx.ListBox(choices=[],id=wxID_FRAME1LISTBOX1, name='listBox1',parent=self,pos=wx.Point(8,48), size=wx.Size(184,256),style=0) self.listBox1.SetBackgroundColour(wx.Colour(255,255,128)) self.listBox1.Bind(wx.EVT_LISTBOX,self.OnListBox1Listbox, id=wxID_FRAME1LISTBOX1) self.button2=wx.Button(id=wxID_FRAME1BUTTON2,label=u'Clear', name='button2',parent=self,pos=wx.Point(104,312), size=wx.Size(87,28),style=0) self.button2.Bind(wx.EVT_BUTTON,self.OnButton2Button, id=wxID_FRAME1BUTTON2) def__init__(self,parent): self._init_ctrls(parent) defOnButton1Button(self,event): ''' clickbuttontoloadthelistboxwithnames ''' self.listBox1.Append("Andreas") self.listBox1.Append("Erich") self.listBox1.Append("Udo") self.listBox1.Append("Jens") self.listBox1.Append("Bjorn") self.listBox1.Append("Heidrun") self.listBox1.Append("Ulla") self.listBox1.Append("Volger") self.listBox1.Append("Helmut") self.listBox1.Append("Freja") self.SetTitle("Selectaname...") defOnListBox1Listbox(self,event): ''' clicklistitemanddisplaytheselectedstringinframe'stitle ''' selName=self.listBox1.GetStringSelection() self.SetTitle(selName) defOnButton2Button(self,event): ''' clickbuttontoclearthelistboxitems ''' self.listBox1.Clear() #---------------endofclassFrame1-------------------- #programentrypoint... if__name__=='__main__': app=wx.PySimpleApp() wx.InitAllImageHandlers() frame=create(None) frame.Show() app.MainLoop()
希望本文所述对大家的Python程序设计有所帮助。