Home arrow Python arrow Page 3 - Checkboxes and Radio Buttons in wxPython

wxRadioButton - Python

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.

TABLE OF CONTENTS:
  1. Checkboxes and Radio Buttons in wxPython
  2. wxCheckListBox
  3. wxRadioButton
  4. wxRadioBox
By: Peyton McCullough
Rating: starstarstarstarstar / 33
August 22, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: