Python
  Home arrow Python arrow Checkboxes and Radio Buttons in wxPython
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Checkboxes and Radio Buttons in wxPython
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 30
    2005-08-22


    Table of Contents:
  • Checkboxes and Radio Buttons in wxPython
  • wxCheckListBox
  • wxRadioButton
  • wxRadioBox

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Checkboxes and Radio Buttons in wxPython
    ( Page 1 of 4 )

    In this article, we will focus on two basic controls and a few relatives: checkboxes and radio buttons. Both controls give the user a variety of options and yet him or her choose anywhere from one to all of them.

    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()



     
     
    >>> More Python Articles          >>> More By Peyton McCullough
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek