Checkboxes and Radio Buttons in wxPython - wxCheckListBox
(Page 2 of 4 )
The wxCheckListBox is a combination of wxCheckBox and wxListBox. It is basically a list of checkboxes that users can click. It's useful if you have a large amount of checkboxes. Let's create an application that shows off wxCheckListBox's features:
from wxPython.wx import *
class Window ( wxFrame ):
def __init__ ( self ):
wxFrame.__init__ ( self, None, -1, 'Dinner', size = ( 300, 300 ) )
# Create a status bar
self.CreateStatusBar()
# Create a panel
self.panel = wxPanel ( self, -1 )
# Create a label
self.label = wxStaticText ( self.panel, -1, 'Configure your dinner:' )
# Define a list of items to place in the wxCheckListBox
self.dinner = [ 'Ravioli', 'Apple Pie', 'Dr. Pepper', 'Soup', 'Salad', 'Ham Sandwich', 'Pizza', \
'Macaroni and Cheese', 'Carrot Cake', 'Spinach', 'Potato Salad ( Southern Style )' ]
# Create a wxCheckListBox
self.box = wxCheckListBox ( self.panel, 100, size = ( 150, 100 ), choices = self.dinner, style = wxLB_HSCROLL )
# Hook up an event handling method
EVT_CHECKLISTBOX ( self.panel, 100, self.clickHandler )
# Create a button
self.button = wxButton ( self.panel, 200, 'Prepare' )
# Hook up an event handling method
EVT_BUTTON ( self.panel, 200, self.buttonHandler )
# Create two sizers to make everything pretty
# The usual
self.vertical = wxBoxSizer ( wxVERTICAL )
self.vertical.Add ( ( 0, 0 ), 1 )
self.vertical.Add ( self.label, 0, wxALIGN_CENTER )
self.vertical.Add ( ( 5, 5 ), 0 )
self.vertical.Add ( self.box, 0, wxALIGN_CENTER )
self.vertical.Add ( ( 5, 5 ), 0 )
self.vertical.Add ( self.button, 0, wxALIGN_CENTER )
self.vertical.Add ( ( 0, 0 ), 1 )
self.horizontal = wxBoxSizer ( wxHORIZONTAL )
self.horizontal.Add ( ( 0, 0 ), 1 )
self.horizontal.Add ( self.vertical, 0, wxALIGN_CENTER )
self.horizontal.Add ( ( 0, 0 ), 1 )
# Attach the sizer
self.panel.SetSizerAndFit ( self.horizontal )
self.Show ( True )
# This method is called when an item is clicked
def clickHandler ( self, event ):
if self.box.IsChecked ( event.GetSelection() ):
message = ' will be prepared.'
else:
message = ' will not be prepared.'
# Update the status bar of the window
self.SetStatusText ( self.box.GetString ( event.GetSelection() ) + message )
# This method will be called when the button is clicked
def buttonHandler ( self, event ):
message = 'The following items have been prepared:'
# Check to see what has been selected and what has not
x = 0
for choice in self.dinner:
if self.box.IsChecked ( x ):
message = message + '\n' + choice
x = x + 1
# Display a dialog
dialog = wxMessageDialog ( self, message, 'Your dinner is ready.', style = wxOK )
dialog.ShowModal()
dialog.Destroy()
application = wxPySimpleApp()
Window()
application.MainLoop()
Next: wxRadioButton >>
More Python Articles
More By Peyton McCullough