Python
  Home arrow Python arrow Page 3 - 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 - wxRadioButton
    ( Page 3 of 4 )

    Radio buttons are useful when you want the user to select one item out of a list of multiple items. Alternatively, the user can check a single radio button that cannot be unchecked. A group of radio buttons is started by setting the style wxRB_GROUP on the first button, and a lone radio button is created by setting wxRB_SINGLE as the style. Let's put together an application that uses radio buttons:

    from wxPython.wx import *

    class Window ( wxFrame ):

       def __init__ ( self ):

          wxFrame.__init__ ( self, None, -1, 'Ballot' )

          # Create a panel, like we always do

          self.panel = wxPanel ( self, -1 )

          # Create a grid layout

          self.sizer = wxGridBagSizer ( 2, 10 )

          # Create one group of buttons ( plus a label ) and add them

          # We mark the beginning of the group with wxRB_GROUP

          self.president1 = wxRadioButton ( self.panel, -1, 'CharlesFagundes', style = wxRB_GROUP )

          self.president2 = wxRadioButton ( self.panel, -1, 'TerriWells' )

          self.sizer.Add ( wxStaticText ( self.panel, -1, 'President' ), ( 0, 0 ) )

          self.sizer.Add ( self.president1, ( 1, 0 ) )

          self.sizer.Add ( self.president2, ( 2, 0 ) )

          # Create another group of radio buttons

          self.senator1 = wxRadioButton ( self.panel, -1, 'Peyton McCullough', style = wxRB_GROUP )

          self.senator2 = wxRadioButton ( self.panel, -1, 'Alejandro Gervasio' )

          self.senator3 = wxRadioButton ( self.panel, -1, 'Jagadish Chatarji' )

          self.senator4 = wxRadioButton ( self.panel, -1, 'Michael Swanson' )

          self.sizer.Add ( wxStaticText ( self.panel, -1, 'Senator' ), ( 0, 1 ) )

          self.sizer.Add ( self.senator1, ( 1, 1 ) )

          self.sizer.Add ( self.senator2, ( 2, 1 ) )

          self.sizer.Add ( self.senator3, ( 3, 1 ) )

          self.sizer.Add ( self.senator4, ( 4, 1 ) )

          # Create yet another group of radio buttons

          # These will not be in a group

          # We'll also create a button, which will be activated if both boxes are checked

          self.term1 = wxRadioButton ( self.panel, 1, 'This is my only ballot.', style = wxRB_SINGLE )

          self.term2 = wxRadioButton ( self.panel, 1, 'I swear it is!', style = wxRB_SINGLE )

          self.button = wxButton ( self.panel, 2, 'Submit' )

          self.button.Disable()

          self.sizer.Add ( wxStaticText ( self.panel, -1, 'Agreement' ), ( 0, 2 ) )

          self.sizer.Add ( self.term1, ( 1, 2 ) )

          self.sizer.Add ( self.term2, ( 2, 2 ) )

          self.sizer.Add ( self.button, ( 3, 2 ), ( 2, 1 ), wxALIGN_CENTER )

          # Hook up events to the three controls above

          EVT_RADIOBUTTON ( self.panel, 1, self.confirm )

          EVT_BUTTON ( self.panel, 2, self.vote )

          # Attach our sizer to our panel

          self.panel.SetSizerAndFit ( self.sizer )

          # Resize the window

          self.sizer.Fit ( self )

          self.Show ( True )

       # This will check to see if term1 and term2 have both been clicked

       # If so, the button will be disabled

       def confirm ( self, event ):

          if ( self.term1.GetValue() == True ) & ( self.term2.GetValue() == True ):

             self.button.Enable()

       # Here, we will send a message to the user and reset everything

       def vote ( self, event ):

          # Show a dialog

          dialog = wxMessageDialog ( self, 'Thank you for your vote.', 'Thank You', style = wxOK )

          dialog.ShowModal()

          dialog.Destroy()

          # Reset things

          self.president1.SetValue ( True )

          self.senator1.SetValue ( True )

          self.term1.SetValue ( False )

          self.term2.SetValue ( False )

          self.button.Disable()

    application = wxPySimpleApp()

    Window()

    application.MainLoop()

    Simple, huh?



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek