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

    By using wxRadioBox, it is possible to create a boxed group of radio buttons. The result looks like a group of radio buttons surrounded by a wxStaticBox and is visually appealing.The creation of a wxRadioBox is incredibly simple because the labels of each radio box are passed in the form of a list. The rows and columns of the wxRadioBox are then determined by the majorDimension variable and either the wxRA_SPECIFY_ROWS or the wxRA_SPECIFY_COLS style. If wxRA_SPECIFY_ROWS is passed, the value in majorDimension will be the number of rows. The same thing works for wxRA_SPECIFY_COLS and columns. Let's take a closer look at how it all works:

    from wxPython.wx import *

    class Window ( wxFrame ):

       def __init__ ( self ):

          wxFrame.__init__ ( self, None, -1, 'wxRadioBox', size = ( 300, 300 ) )

          # Create a status bar

          self.CreateStatusBar()

          # Create a panel

          self.panel = wxPanel ( self, -1 )

          # Create a list of radio buttons

          self.radioList = [ 'This', 'Is', 'A', 'Box', 'Full', 'Of', 'Radio', 'Buttons' ]

          # Create a wxRadioBox with two rows

          self.radioBox1 = wxRadioBox ( self.panel, 1, 'Radio Box 1', choices = self.radioList, majorDimension = 2, style = wxRA_SPECIFY_ROWS )

          # Create a wxRadoiBox with two columns

          self.radioBox2 = wxRadioBox ( self.panel, 1, 'Radio Box 2', choices = self.radioList, majorDimension = 2, style = wxRA_SPECIFY_COLS )

          # Tie in methods to handle a click

          EVT_RADIOBOX ( self.panel, 1, self.radioClick )

          # Center everything

          self.vertical = wxBoxSizer ( wxVERTICAL )

          self.vertical.Add ( ( 0, 0 ), 1 )

          self.vertical.Add ( self.radioBox1, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 5, 5 ), 0 )

          self.vertical.Add ( self.radioBox2, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 0, 0 ), 1 )

          self.horizontal = wxBoxSizer ( wxHORIZONTAL )

          self.horizontal.Add ( ( 0, 0 ), 1 )

          self.horizontal.Add ( self.vertical, 0, wxALIGN_CENTER )

          self.horizontal.Add ( ( 0, 0 ), 1 )

          # Attach the sizer

          self.panel.SetSizerAndFit ( self.horizontal )

          self.Show ( True )

       def radioClick ( self, event ):

          # Get the wxRadioBox

          radioBox = event.GetEventObject()

          # Get the index of the selected button

          radioIndex = radioBox.GetSelection()

          # Get the string of the selected button

          radioString = radioBox.GetStringSelection()

          # Update the status bar

          self.SetStatusText ( str ( radioIndex ) + ': ' + radioString )

    application = wxPySimpleApp()

    Window()

    application.MainLoop()

    There are other things you can do with a wxRadioBox, too. You can disable a wxRadioBox, find the location of a label, count the buttons, get the label of a button, set the label of a button and set the current selection. Let's take a look at these features:

    from wxPython.wx import *

    class Window ( wxFrame ):

       def __init__ ( self ):

          wxFrame.__init__ ( self, None, -1, 'wxRadioBox Features', size = ( 300, 300 ) )

          # Create a panel

          self.panel = wxPanel ( self, -1 )

          # Create a wxRadioBox to experiment on

          self.box = wxRadioBox ( self.panel, 100, 'A Radio Box', choices = [ 'A', 'B', 'C', 'D' ], majorDimension = 2, style = wxRA_SPECIFY_COLS )

          # Handle a click with a method

          EVT_RADIOBOX ( self.panel, 100, self.radioClick )

          # Create a label to count the items in our wxRadioBox

          self.count = wxStaticText ( self.panel, -1, 'Count: ' + str ( self.box.GetCount() ) )

          # Create a label to display the label of the currently selected button

          self.label = wxStaticText ( self.panel, -1, 'Label: ' + str ( self.box.GetItemLabel ( self.box.GetSelection() ) ) )

          # Create a label to display the position of the radio button whose label is "A":

          self.find = wxStaticText ( self.panel, -1, '"A": ' + str ( self.box.FindString ( 'A' ) ) )

          # Create a button that disables the entire wxRadioBox and attach a handler

          self.disableBoxButton = wxButton ( self.panel, 200, 'Disable Box' )

          EVT_BUTTON ( self.panel, 200, self.disableBox )

          # Create a button that disables the currently selected item and attach a handler

          self.disableItemButton = wxButton ( self.panel, 300, 'Disable Item' )

          EVT_BUTTON ( self.panel, 300, self.disableItem )

          # Create a button that changes the label of the currently selected item and attach a handler

          self.changeLabelButton = wxButton ( self.panel, 400, 'Change Label' )

          EVT_BUTTON ( self.panel, 400, self.changeLabel )

          # Center things in two sizers

          self.vertical = wxBoxSizer ( wxVERTICAL )

          self.vertical.Add ( ( 0, 0 ), 1 )

          self.vertical.Add ( self.box, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 5, 5 ), 0 )

          self.vertical.Add ( self.count, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 5, 5 ), 0 )

          self.vertical.Add ( self.label, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 5, 5 ), 0 )

          self.vertical.Add ( self.find, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 5, 5 ), 0 )

          self.vertical.Add ( self.disableBoxButton, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 5, 5 ), 0 )

          self.vertical.Add ( self.disableItemButton, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 5, 5 ), 0 )

          self.vertical.Add ( self.changeLabelButton, 0, wxALIGN_CENTER )

          self.vertical.Add ( ( 0, 0 ), 1 )

          self.horizontal = wxBoxSizer ( wxHORIZONTAL )

          self.horizontal.Add ( ( 0, 0 ), 1 )

          self.horizontal.Add ( self.vertical, 0, wxALIGN_CENTER )

          self.horizontal.Add ( ( 0, 0 ), 1 )

          # Attach our horizontal sizer

          self.panel.SetSizerAndFit ( self.horizontal )

          self.Show ( True )

       # This method is called when a radio button is clicked

       def radioClick ( self, event ):

          # Update the label label

          self.label.SetLabel ( 'Label: ' + str ( self.box.GetItemLabel ( self.box.GetSelection() ) ) )

       # This method is called when the "Disable Box" button is clicked

       def disableBox ( self, event ):

          # Disable the wxRadioBox

          self.box.Enable ( False )

       # This method is called when the "Disable Item" button is clicked

       def disableItem ( self, event ):

          # Disable the currently selected item

          self.box.EnableItem ( self.box.GetSelection(), False )

       # This method is called when the "Set Label" button is clicked

       def changeLabel ( self, event ):

          # Set the label of the currently selected button to "Q"

          self.box.SetItemLabel ( self.box.GetSelection(), 'Q' )

          # Update the find label

          self.find.SetLabel ( '"A": ' + str ( self.box.FindString ( 'A' ) ) )

    application = wxPySimpleApp()

    Window()

    application.MainLoop()

    Conclusion

    Several “box” controls are offered in wxPython, and they can serve multiple purposes as I mentioned earlier. It's all up to your creativity. These controls are very simple to use, featuring only a few methods and styles, yet they offer you a large amount of flexibility. These controls can improve your applications when necessary, pleasing their users – the goal of your graphical user interfaces.



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