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.