]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/RadioButton.py
   4 #---------------------------------------------------------------------- 
   6 class TestPanel( wx
.Panel 
): 
   7     def __init__( self
, parent
, log 
): 
   9         wx
.Panel
.__init
__( self
, parent
, -1 ) 
  11         panel 
= wx
.Panel( self
, -1 ) 
  13         # Layout controls on panel: 
  14         vs 
= wx
.BoxSizer( wx
.VERTICAL 
) 
  16         box1_title 
= wx
.StaticBox( panel
, -1, "Group 1" ) 
  17         box1 
= wx
.StaticBoxSizer( box1_title
, wx
.VERTICAL 
) 
  18         grid1 
= wx
.FlexGridSizer( 0, 2, 0, 0 ) 
  20         # 1st group of controls: 
  21         self
.group1_ctrls 
= [] 
  22         radio1 
= wx
.RadioButton( panel
, -1, " Radio1 ", style 
= wx
.RB_GROUP 
) 
  23         radio2 
= wx
.RadioButton( panel
, -1, " Radio2 " ) 
  24         radio3 
= wx
.RadioButton( panel
, -1, " Radio3 " ) 
  25         text1 
= wx
.TextCtrl( panel
, -1, "" ) 
  26         text2 
= wx
.TextCtrl( panel
, -1, "" ) 
  27         text3 
= wx
.TextCtrl( panel
, -1, "" ) 
  28         self
.group1_ctrls
.append((radio1
, text1
)) 
  29         self
.group1_ctrls
.append((radio2
, text2
)) 
  30         self
.group1_ctrls
.append((radio3
, text3
)) 
  32         for radio
, text 
in self
.group1_ctrls
: 
  33             grid1
.Add( radio
, 0, wx
.ALIGN_CENTRE|wx
.LEFT|wx
.RIGHT|wx
.TOP
, 5 ) 
  34             grid1
.Add( text
, 0, wx
.ALIGN_CENTRE|wx
.LEFT|wx
.RIGHT|wx
.TOP
, 5 ) 
  36         box1
.Add( grid1
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 ) 
  37         vs
.Add( box1
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 ) 
  39         box2_title 
= wx
.StaticBox( panel
, -1, "Group 2" ) 
  40         box2 
= wx
.StaticBoxSizer( box2_title
, wx
.VERTICAL 
) 
  41         grid2 
= wx
.FlexGridSizer( 0, 2, 0, 0 ) 
  43         # 2nd group of controls: 
  44         self
.group2_ctrls 
= [] 
  45         radio4 
= wx
.RadioButton( panel
, -1, " Radio1 ", style 
= wx
.RB_GROUP 
) 
  46         radio5 
= wx
.RadioButton( panel
, -1, " Radio2 " ) 
  47         radio6 
= wx
.RadioButton( panel
, -1, " Radio3 " ) 
  48         text4 
= wx
.TextCtrl( panel
, -1, "" ) 
  49         text5 
= wx
.TextCtrl( panel
, -1, "" ) 
  50         text6 
= wx
.TextCtrl( panel
, -1, "" ) 
  51         self
.group2_ctrls
.append((radio4
, text4
)) 
  52         self
.group2_ctrls
.append((radio5
, text5
)) 
  53         self
.group2_ctrls
.append((radio6
, text6
)) 
  55         for radio
, text 
in self
.group2_ctrls
: 
  56             grid2
.Add( radio
, 0, wx
.ALIGN_CENTRE|wx
.LEFT|wx
.RIGHT|wx
.TOP
, 5 ) 
  57             grid2
.Add( text
, 0, wx
.ALIGN_CENTRE|wx
.LEFT|wx
.RIGHT|wx
.TOP
, 5 ) 
  59         box2
.Add( grid2
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 ) 
  60         vs
.Add( box2
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 ) 
  67         # Setup event handling and initial state for controls: 
  68         for radio
, text 
in self
.group1_ctrls
: 
  69             self
.Bind(wx
.EVT_RADIOBUTTON
, self
.OnGroup1Select
, radio 
) 
  71         for radio
, text 
in self
.group2_ctrls
: 
  72             self
.Bind(wx
.EVT_RADIOBUTTON
, self
.OnGroup2Select
, radio 
) 
  74         for radio
, text 
in self
.group1_ctrls 
+ self
.group2_ctrls
: 
  78     def OnGroup1Select( self
, event 
): 
  79         radio_selected 
= event
.GetEventObject() 
  80         self
.log
.write('Group1 %s selected\n' % radio_selected
.GetLabel() ) 
  82         for radio
, text 
in self
.group1_ctrls
: 
  83             if radio 
is radio_selected
: 
  88     def OnGroup2Select( self
, event 
): 
  89         radio_selected 
= event
.GetEventObject() 
  90         self
.log
.write('Group2 %s selected\n' % radio_selected
.GetLabel() ) 
  92         for radio
, text 
in self
.group2_ctrls
: 
  93             if radio 
is radio_selected
: 
  98 #---------------------------------------------------------------------- 
 100 def runTest( frame
, nb
, log 
): 
 101     win 
= TestPanel( nb
, log 
) 
 104 #---------------------------------------------------------------------- 
 110 This demo shows how individual radio buttons can be used to build 
 111 more complicated selection mechanisms... 
 113 It uses 2 groups of wx.RadioButtons, where the groups are defined by 
 114 instantiation.  When a wx.RadioButton is created with the <I>wx.RB_GROUP</I> 
 115 style, all subsequent wx.RadioButtons created without it are implicitly 
 116 added to that group by the framework. 
 122 if __name__ 
== '__main__': 
 125     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])