Python
  Home arrow Python arrow Page 3 - Checkboxes and Radio Buttons in wxPyth...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PYTHON

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

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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Too complicated
       · I would not label it as complicated at all, and using .NET would mean losing...
     

       

    PYTHON ARTICLES

    - 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
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT