wxPython - RadioButton & RadioBox


Advertisements

A Radio button usually represents one of many selectable buttons available for the user in a group. Each button, an object of wx.RadioButton class carries a text label next to a round button.

In order to create a group of mutually selectable buttons, the style parameter of first wxRadioButton object is set to wx.RB_GROUP. Subsequent button objects are added to a group.

wxPython API also consists of wx.RadioBox class. Its object offers a border and label to the group. Buttons in the group can be arranged horizontally or vertically.

wx.RadioButton constructor looks like −

Wx.RadioButton(parent, id, label, pos, size, style)

The style parameter is present only for the first button in the group. Its value is wx.RB_GROUP. For subsequent buttons in the group, wx.RB_SINGLE style parameter may be optionally used.

wx.RadioButton event binder wx.EVT_RADIOBUTTON triggers the associated handler every time any of the buttons in the group is clicked.

Two important methods of wx.RadioButton class are SetValue() – to select or deselect a button programmatically – and GetValue() – which returns true if a button is selected and is false otherwise.

A wx.RadioBox places a collection of mutually exclusive buttons in a static box. Each button in the group takes its label from a List object which acts as ‘choices’ parameter for wx.RadioBox constructor.

Buttons in RadioBox are laid out in row-wise or column-wise manner. For that ‘style’ parameter of constructor should be either wx.RA_SPECIFY_ROWS or wx.RA_SPECIFY_COLS. Number of rows/columns is decided by the value of ‘majordimensions’ parameter.

Prototype of wx.RadioBox constructor is −

Wx.RadioBox(parent, id, label, pos, size, choices[], initialdimensions, style)

Following are the important methods in wx.RadioBox class −

S.N. Methods & Description
1

GetSelection()

Returns the index of the selected item

2

SetSelection()

Selects an item programmatically

3

GetString()

Returns the label of the selected item

4

SetString()

Assigns the label to the selected item

5

Show()

Shows or hides the item of the given index

Event binder associated with wx.RadioBox object is wx.EVT_RADIOBOX. Associated event handler identifies the button selection and processes it.

Example

The following example demonstrates the use of RadioBox as well as RadioButton. Firstly, three RadioButtons, grouped by specifying wx.RB_GROUP style are placed on the panel.

self.rb1 = wx.RadioButton(pnl,11, label = 'Value A', pos = (10,10), style = wx.RB_GROUP) 
self.rb2 = wx.RadioButton(pnl,22, label = 'Value B',pos = (10,40)) 
self.rb3 = wx.RadioButton(pnl,33, label = 'Value C',pos = (10,70))

The RadioBox, on the other hand, reads labels for its buttons from a lblList[] object.

lblList = ['Value X', 'Value Y', 'Value Z']     
self.rbox = wx.RadioBox(pnl,label = 'RadioBox', pos = (80,10), choices = lblList ,
   majorDimension = 1, style = wx.RA_SPECIFY_ROWS)

Two event binders, one for radio group and other for RadioBox, are declared.

self.Bind(wx.EVT_RADIOBUTTON, self.OnRadiogroup) 
self.rbox.Bind(wx.EVT_RADIOBOX,self.onRadioBox)

The corresponding event handlers identify the button selected and display the message on the console window.

def OnRadiogroup(self, e): 
   rb = e.GetEventObject() 
   print rb.GetLabel(),' is clicked from Radio Group' 
	
def onRadioBox(self,e): 
   print self.rbox.GetStringSelection(),' is clicked from Radio Box'

The complete code is as follows −

import wx   

class Example(wx.Frame): 
            
   def __init__(self, parent, title): 
      super(Example, self).__init__(parent, title = title,size = (300,200)) 
         
      self.InitUI() 
		
   def InitUI(self):    
      pnl = wx.Panel(self)
		
      self.rb1 = wx.RadioButton(pnl,11, label = 'Value A',
         pos = (10,10), style = wx.RB_GROUP) 
      self.rb2 = wx.RadioButton(pnl,22, label = 'Value B',pos = (10,40)) 
      self.rb3 = wx.RadioButton(pnl,33, label = 'Value C',pos = (10,70)) 
      self.Bind(wx.EVT_RADIOBUTTON, self.OnRadiogroup)
		
      lblList = ['Value X', 'Value Y', 'Value Z'] 
		  
      self.rbox = wx.RadioBox(pnl, label = 'RadioBox', pos = (80,10), choices = lblList,
         majorDimension = 1, style = wx.RA_SPECIFY_ROWS) 
      self.rbox.Bind(wx.EVT_RADIOBOX,self.onRadioBox) 
         
      self.Centre() 
      self.Show(True)    
		
   def OnRadiogroup(self, e): 
      rb = e.GetEventObject() 
      print rb.GetLabel(),' is clicked from Radio Group' 
		
   def onRadioBox(self,e): 
      print self.rbox.GetStringSelection(),' is clicked from Radio Box' 
   
ex = wx.App() 
Example(None,'RadioButton and RadioBox') 
ex.MainLoop()

The above code produces the following output −

Radio Button Output

Value B is clicked from Radio Group

Value C is clicked from Radio Group

Value Y is clicked from Radio Box

Value X is clicked from Radio Box

wxpython_major_classes.htm
Advertisements