A Close Look at wxPython Controls - wxListBox is versatile
(Page 3 of 5 )
The wxListBox control supports multiple selections. We must handle this differently as well:
from wxPython.wx import *
class OurFrame ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'wxPython' )
self.panel = wxPanel ( self, -1 )
self.list = wxListBox ( self.panel, -1, choices = [ '1', '2', '3', '4' ], style = wxLB_MULTIPLE )
self.button = wxButton ( self.panel, 100, 'Click Me...now.' )
EVT_BUTTON ( self.panel, 100, self.click )
self.sizer = wxBoxSizer ( wxVERTICAL )
self.sizer.Add ( self.list, 0 )
self.sizer.Add ( self.button, 0 )
self.panel.SetSizerAndFit ( self.sizer )
self.Show ( True )
def click ( self, event ):
selections = self.list.GetSelections()
if len ( selections ) > 1:
for selection in selections:
print 'Multiple Selected:', self.list.GetString ( selection )
else:
print 'Single Selected:', self.list.GetString ( selections [ 0 ] )
application = wxPySimpleApp()
window = OurFrame()
application.MainLoop()
We can insert a whole list of items rather than one at a time, and we can also reset the list entirely, without using Clear:
from wxPython.wx import *
class OurFrame ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'wxPython' )
self.panel = wxPanel ( self, -1 )
self.list = wxListBox ( self.panel, -1, choices = [ '1', '2', '3', '4' ] )
# Reset it
self.list.Set ( [ 'Choice 1', 'Choice 2', 'Choice 3', 'Choice 4' ] )
# Add some more items
self.list.InsertItems ( [ 'Choice 5', 'Choice 6', 'Choice 7', 'Choice 8', 'Choice 9', 'Choice 10' ], 4 )
self.lilst.InsertItems ( [ 'Choice 0' ], 0 )
self.sizer = wxBoxSizer ( wxVERTICAL )
self.sizer.Add ( self.list, 0 )
self.panel.SetSizerAndFit ( self.sizer )
self.Show ( True )
application = wxPySimpleApp()
window = OurFrame()
application.MainLoop()
Finally, let's look at some events:
from wxPython.wx import *
class OurFrame ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'wxPython' )
self.panel = wxPanel ( self, -1 )
self.list = wxListBox ( self.panel, 100, choices = [ 'One', 'Two', 'Three', 'Four' ] )
self.combo = wxComboBox ( self.panel, 200, choices = [ 'Red', 'Blue', 'Green', 'Pink' ] )
EVT_LISTBOX ( self.panel, 100, self.click)
EVT_LISTBOX_DCLICK ( self.panel, 100, self.listDouble )
EVT_COMBOBOX ( self.panel, 200, self.click )
EVT_TEXT_ENTER ( self.panel, 200, self.comboEnter )
EVT_TEXT ( self.panel, 200, self.comboText )
self.sizer = wxBoxSizer ( wxVERTICAL )
self.sizer.Add ( self.list, 0 )
self.sizer.Add ( self.combo, 0 )
self.panel.SetSizerAndFit ( self.sizer )
self.Show ( True )
def click ( self, event ):
print 'Single click.'
def listDouble ( self, event ):
print 'Double click.'
def comboEnter ( self, event ):
print 'You pressed enter in the wxComboBox.'
def comboText ( self, event ):
print 'You changed the text in the wxComboBox.'
application = wxPySimpleApp()
window = OurFrame()
application.MainLoop()
Next: wxTextCtrl >>
More Python Articles
More By Peyton McCullough