1 from wxPython
.wx
import *
2 #----------------------------------------------------------------------
4 class TestPanel( wxPanel
):
5 def __init__( self
, parent
, log
):
7 wxPanel
.__init
__( self
, parent
, -1 )
9 panel
= wxPanel( self
, -1 )
11 # 1st group of controls:
12 self
.group1_ctrls
= []
13 radio1
= wxRadioButton( panel
, -1, " Radio1 ", style
= wxRB_GROUP
)
14 text1
= wxTextCtrl( panel
, -1, "" )
15 radio2
= wxRadioButton( panel
, -1, " Radio2 " )
16 text2
= wxTextCtrl( panel
, -1, "" )
17 radio3
= wxRadioButton( panel
, -1, " Radio3 " )
18 text3
= wxTextCtrl( panel
, -1, "" )
19 self
.group1_ctrls
.append((radio1
, text1
))
20 self
.group1_ctrls
.append((radio2
, text2
))
21 self
.group1_ctrls
.append((radio3
, text3
))
23 # 2nd group of controls:
24 self
.group2_ctrls
= []
25 radio4
= wxRadioButton( panel
, -1, " Radio1 ", style
= wxRB_GROUP
)
26 text4
= wxTextCtrl( panel
, -1, "" )
27 radio5
= wxRadioButton( panel
, -1, " Radio2 " )
28 text5
= wxTextCtrl( panel
, -1, "" )
29 radio6
= wxRadioButton( panel
, -1, " Radio3 " )
30 text6
= wxTextCtrl( panel
, -1, "" )
31 self
.group2_ctrls
.append((radio4
, text4
))
32 self
.group2_ctrls
.append((radio5
, text5
))
33 self
.group2_ctrls
.append((radio6
, text6
))
35 # Layout controls on panel:
36 vs
= wxBoxSizer( wxVERTICAL
)
38 box1_title
= wxStaticBox( panel
, -1, "Group 1" )
39 box1
= wxStaticBoxSizer( box1_title
, wxVERTICAL
)
40 grid1
= wxFlexGridSizer( 0, 2, 0, 0 )
41 for radio
, text
in self
.group1_ctrls
:
42 grid1
.AddWindow( radio
, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP
, 5 )
43 grid1
.AddWindow( text
, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP
, 5 )
44 box1
.AddSizer( grid1
, 0, wxALIGN_CENTRE|wxALL
, 5 )
45 vs
.AddSizer( box1
, 0, wxALIGN_CENTRE|wxALL
, 5 )
47 box2_title
= wxStaticBox( panel
, -1, "Group 2" )
48 box2
= wxStaticBoxSizer( box2_title
, wxVERTICAL
)
49 grid2
= wxFlexGridSizer( 0, 2, 0, 0 )
50 for radio
, text
in self
.group2_ctrls
:
51 grid2
.AddWindow( radio
, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP
, 5 )
52 grid2
.AddWindow( text
, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP
, 5 )
53 box2
.AddSizer( grid2
, 0, wxALIGN_CENTRE|wxALL
, 5 )
54 vs
.AddSizer( box2
, 0, wxALIGN_CENTRE|wxALL
, 5 )
61 # Setup event handling and initial state for controls:
62 for radio
, text
in self
.group1_ctrls
:
63 EVT_RADIOBUTTON( self
, radio
.GetId(), self
.OnGroup1Select
)
65 for radio
, text
in self
.group2_ctrls
:
66 EVT_RADIOBUTTON( self
, radio
.GetId(), self
.OnGroup2Select
)
68 for radio
, text
in self
.group1_ctrls
+ self
.group2_ctrls
:
73 def OnGroup1Select( self
, event
):
74 radio_selected
= event
.GetEventObject()
75 self
.log
.write('Group1 %s selected\n' % radio_selected
.GetLabel() )
76 for radio
, text
in self
.group1_ctrls
:
77 if radio
is radio_selected
:
82 def OnGroup2Select( self
, event
):
83 radio_selected
= event
.GetEventObject()
84 self
.log
.write('Group2 %s selected\n' % radio_selected
.GetLabel() )
85 for radio
, text
in self
.group2_ctrls
:
86 if radio
is radio_selected
:
91 #----------------------------------------------------------------------
93 def runTest( frame
, nb
, log
):
94 win
= TestPanel( nb
, log
)
97 #----------------------------------------------------------------------
103 This demo shows how individual radio buttons can be used to build
104 more complicated selection mechanisms...
106 It uses 2 groups of wxRadioButtons, where the groups are defined by
107 instantiation. When a wxRadioButton is created with the <I>wxRB_GROUP</I>
108 style, all subsequent wxRadioButtons created without it are implicitly
109 added to that group by the framework.
115 if __name__
== '__main__':
118 run
.main(['', os
.path
.basename(sys
.argv
[0])])