This article assumes that you have basic knowledge of wxPython – how to create a frame, populate it with controls and organize them. Make sure you understand these tasks before you dive into this article. wxCheckBox Checkboxes are useful in applications. Besides looking nice and neat, they can be used for more than one purpose. For example, in one application, checkboxes could allow a user to select all the items that apply, and in another application, checkboxes could give the user the option to turn things on and off. They are also very easy to work with and aren't over-complicated by too many methods and events. Let's take a look at a very simple application that uses checkboxes to gather input from the user: from wxPython.wx import * class Window ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'Survey', size = ( 400, 200 ) ) # Create a panel to house everything self.panel = wxPanel ( self, -1 ) # Create a wxGridBagSizer to organize our widgets self.sizer = wxGridBagSizer ( 5, 5 ) # Create a label for our application self.label = wxStaticText ( self.panel, -1, 'What electronics do you plan to buy in the next year?' + \ '\nCheck all that apply.', style = wxTE_CENTER ) # Create a few wxCheckBox instances self.camera = wxCheckBox ( self.panel, -1, 'Digital Camera' ) self.music = wxCheckBox ( self.panel, -1, 'Music-related Device' ) self.hardware = wxCheckBox ( self.panel, -1, 'Computer Hardware' ) self.television = wxCheckBox ( self.panel, -1, 'Television' ) self.dvd = wxCheckBox ( self.panel, -1, 'DVDPlayer' ) self.other = wxCheckBox ( self.panel, -1, 'Other' ) # Create a button self.button = wxButton ( self.panel, 100, 'Submit' ) # Catch a click to the button EVT_BUTTON ( self.panel, 100, self.submit ) # Add everything to the sizer self.sizer.Add ( self.label, ( 0, 0 ), ( 1, 3 ), wxALIGN_CENTER ) self.sizer.Add ( self.camera, ( 1, 0 ) ) self.sizer.Add ( self.music, ( 1, 1 ) ) self.sizer.Add ( self.hardware, ( 1, 2 ) ) self.sizer.Add ( self.television, ( 2, 0 ) ) self.sizer.Add ( self.dvd, ( 2, 1 ) ) self.sizer.Add ( self.other, ( 2, 2 ) ) self.sizer.Add ( self.button, ( 3, 0 ), ( 1, 3 ), wxALIGN_CENTER ) # Center everything self.horizontal = wxBoxSizer ( wxHORIZONTAL ) self.horizontal.Add ( ( 0, 0 ), 1 ) self.horizontal.Add ( self.sizer ) self.horizontal.Add ( ( 0, 0 ), 1 ) self.vertical = wxBoxSizer ( wxVERTICAL ) self.vertical.Add ( ( 0, 0, ), 1 ) self.vertical.Add ( self.horizontal, 0, wxALIGN_CENTER ) self.vertical.Add ( ( 0, 0 ), 1 ) # Attach the sizer to the panel self.panel.SetSizerAndFit ( self.vertical ) self.Show ( True ) # This method is triggered when the button is clicked # It checks whether or not each checkbox is clicked def submit ( self, event ): message = 'You plan to get the following items in the next year:' # Check whether or not each checkbox is clicked # If one is, add a string to the message variable if self.camera.GetValue(): message = message + '\nDigital Camera' if self.music.GetValue(): message = message + '\nMusic-related Device' if self.hardware.GetValue(): message = message + '\nComputer Hardware' if self.television.GetValue(): message = message + '\nTelevision' if self.dvd.GetValue(): message = message + '\nDVD Player' if self.other.GetValue(): message = message + '\nOther' # Display a dialog with the results dialog = wxMessageDialog ( self, message, 'Results', style = wxOK ) dialog.ShowModal() dialog.Destroy() application = wxPySimpleApp() Window() application.MainLoop() It is also possible to catch a click on a checkbox and set the value of the checkbox. We'll create a simple application that combines both of these functions: from wxPython.wx import * class Window ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'Checkbox Test', size = ( 200, 200 ) ) # Create a panel self.panel = wxPanel ( self, -1 ) # Create a vertical sizer self.vertical = wxBoxSizer ( wxVERTICAL ) # Add a space self.vertical.Add ( ( 0, 0 ), 1 ) # Create and add two checkboxes self.first = wxCheckBox ( self.panel, 1, 'First' ) self.second = wxCheckBox ( self.panel, 2, 'Second' ) self.vertical.Add ( self.first, 0, wxALIGN_CENTER ) self.vertical.Add ( ( 5, 5 ), 0 ) self.vertical.Add ( self.second, 0, wxALIGN_CENTER ) # Catch events EVT_CHECKBOX ( self.panel, 1, self.firstHandle ) EVT_CHECKBOX ( self.panel, 2, self.secondHandle ) # Add another space self.vertical.Add ( ( 0, 0 ), 1 ) # Center it with a horizontal sizer self.horizontal = wxBoxSizer ( wxHORIZONTAL ) self.horizontal.Add ( ( 0, 0 ), 1 ) self.horizontal.Add ( self.vertical, 0, wxALIGN_CENTER ) self.horizontal.Add ( ( 0, 0 ), 1 ) # Add the sizer self.panel.SetSizerAndFit ( self.horizontal ) self.Show ( True ) # This method unchecks the other checkbox if it is checked def firstHandle ( self, event ): self.second.SetValue ( False ) # This method does the same thing def secondHandle ( self, event ): self.first.SetValue ( False ) application = wxPySimpleApp() Window() application.MainLoop() A normal checkbox has two states. It is possible, however, to create a checkbox with three states: from wxPython.wx import * class Window ( wxFrame ): def __init__ ( self ): wxFrame.__init__ ( self, None, -1, 'Checkbox Test', size = ( 200, 200 ) ) # Create a panel self.panel = wxPanel ( self, -1 ) # Create a checkbox with three states self.checkbox = wxCheckBox ( self.panel, 1, 'Tri-state border', style = wxCHK_3STATE | wxCHK_ALLOW_3RD_STATE_FOR_USER ) # Catch a click EVT_CHECKBOX ( self.panel, 1, self.handle ) self.Show ( True ) # Handle a click event def handle ( self, event ): # Print the value of the checkbox print self.checkbox.Get3StateValue() application = wxPySimpleApp() Window() application.MainLoop()
blog comments powered by Disqus |
|
|
|
|
|
|
|