]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/RadioButton.py
ad4ee8ce7c250df1be07de478697a43df1655649
[wxWidgets.git] / wxPython / demo / RadioButton.py
1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import wx
7
8 #----------------------------------------------------------------------
9
10 class TestPanel( wx.Panel ):
11 def __init__( self, parent, log ):
12
13 wx.Panel.__init__( self, parent, -1 )
14 self.log = log
15 panel = wx.Panel( self, -1 )
16
17 # 1st group of controls:
18 self.group1_ctrls = []
19 radio1 = wx.RadioButton( panel, -1, " Radio1 ", style = wx.RB_GROUP )
20 text1 = wx.TextCtrl( panel, -1, "" )
21 radio2 = wx.RadioButton( panel, -1, " Radio2 " )
22 text2 = wx.TextCtrl( panel, -1, "" )
23 radio3 = wx.RadioButton( panel, -1, " Radio3 " )
24 text3 = wx.TextCtrl( panel, -1, "" )
25 self.group1_ctrls.append((radio1, text1))
26 self.group1_ctrls.append((radio2, text2))
27 self.group1_ctrls.append((radio3, text3))
28
29 # 2nd group of controls:
30 self.group2_ctrls = []
31 radio4 = wx.RadioButton( panel, -1, " Radio1 ", style = wx.RB_GROUP )
32 text4 = wx.TextCtrl( panel, -1, "" )
33 radio5 = wx.RadioButton( panel, -1, " Radio2 " )
34 text5 = wx.TextCtrl( panel, -1, "" )
35 radio6 = wx.RadioButton( panel, -1, " Radio3 " )
36 text6 = wx.TextCtrl( panel, -1, "" )
37 self.group2_ctrls.append((radio4, text4))
38 self.group2_ctrls.append((radio5, text5))
39 self.group2_ctrls.append((radio6, text6))
40
41 # Layout controls on panel:
42 vs = wx.BoxSizer( wx.VERTICAL )
43
44 box1_title = wx.StaticBox( panel, -1, "Group 1" )
45 box1 = wx.StaticBoxSizer( box1_title, wx.VERTICAL )
46 grid1 = wx.FlexGridSizer( 0, 2, 0, 0 )
47
48 for radio, text in self.group1_ctrls:
49 grid1.AddWindow( radio, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
50 grid1.AddWindow( text, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
51
52 box1.AddSizer( grid1, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
53 vs.AddSizer( box1, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
54
55 box2_title = wx.StaticBox( panel, -1, "Group 2" )
56 box2 = wx.StaticBoxSizer( box2_title, wx.VERTICAL )
57 grid2 = wx.FlexGridSizer( 0, 2, 0, 0 )
58
59 for radio, text in self.group2_ctrls:
60 grid2.AddWindow( radio, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
61 grid2.AddWindow( text, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
62
63 box2.AddSizer( grid2, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
64 vs.AddSizer( box2, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
65
66 panel.SetSizer( vs )
67 vs.Fit( panel )
68 panel.Move( (50,50) )
69 self.panel = panel
70
71 # Setup event handling and initial state for controls:
72 for radio, text in self.group1_ctrls:
73 self.Bind(wx.EVT_RADIOBUTTON, self.OnGroup1Select, radio )
74
75 for radio, text in self.group2_ctrls:
76 self.Bind(wx.EVT_RADIOBUTTON, self.OnGroup2Select, radio )
77
78 for radio, text in self.group1_ctrls + self.group2_ctrls:
79 radio.SetValue(0)
80 text.Enable(False)
81
82 def OnGroup1Select( self, event ):
83 radio_selected = event.GetEventObject()
84 self.log.write('Group1 %s selected\n' % radio_selected.GetLabel() )
85
86 for radio, text in self.group1_ctrls:
87 if radio is radio_selected:
88 text.Enable(True)
89 else:
90 text.Enable(False)
91
92 def OnGroup2Select( self, event ):
93 radio_selected = event.GetEventObject()
94 self.log.write('Group2 %s selected\n' % radio_selected.GetLabel() )
95
96 for radio, text in self.group2_ctrls:
97 if radio is radio_selected:
98 text.Enable(True)
99 else:
100 text.Enable(False)
101
102 #----------------------------------------------------------------------
103
104 def runTest( frame, nb, log ):
105 win = TestPanel( nb, log )
106 return win
107
108 #----------------------------------------------------------------------
109
110
111 overview = """\
112 <html><body>
113 <P>
114 This demo shows how individual radio buttons can be used to build
115 more complicated selection mechanisms...
116 <P>
117 It uses 2 groups of wxRadioButtons, where the groups are defined by
118 instantiation. When a wxRadioButton is created with the <I>wxRB_GROUP</I>
119 style, all subsequent wxRadioButtons created without it are implicitly
120 added to that group by the framework.
121 </body></html>
122 """
123
124
125
126 if __name__ == '__main__':
127 import sys,os
128 import run
129 run.main(['', os.path.basename(sys.argv[0])])
130