]>
Commit | Line | Data |
---|---|---|
1 | from wxPython.wx import * | |
2 | #---------------------------------------------------------------------- | |
3 | ||
4 | class TestPanel( wxPanel ): | |
5 | def __init__( self, parent, log ): | |
6 | ||
7 | wxPanel.__init__( self, parent, -1 ) | |
8 | self.log = log | |
9 | panel = wxPanel( self, -1 ) | |
10 | ||
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)) | |
22 | ||
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)) | |
34 | ||
35 | # Layout controls on panel: | |
36 | vs = wxBoxSizer( wxVERTICAL ) | |
37 | ||
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 ) | |
46 | ||
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 ) | |
55 | ||
56 | panel.SetSizer( vs ) | |
57 | vs.Fit( panel ) | |
58 | panel.Move( (50,50) ) | |
59 | self.panel = panel | |
60 | ||
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 ) | |
64 | ||
65 | for radio, text in self.group2_ctrls: | |
66 | EVT_RADIOBUTTON( self, radio.GetId(), self.OnGroup2Select ) | |
67 | ||
68 | for radio, text in self.group1_ctrls + self.group2_ctrls: | |
69 | radio.SetValue(0) | |
70 | text.Enable(False) | |
71 | ||
72 | ||
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: | |
78 | text.Enable(True) | |
79 | else: | |
80 | text.Enable(False) | |
81 | ||
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: | |
87 | text.Enable(True) | |
88 | else: | |
89 | text.Enable(False) | |
90 | ||
91 | #---------------------------------------------------------------------- | |
92 | ||
93 | def runTest( frame, nb, log ): | |
94 | win = TestPanel( nb, log ) | |
95 | return win | |
96 | ||
97 | #---------------------------------------------------------------------- | |
98 | ||
99 | ||
100 | overview = """\ | |
101 | <html><body> | |
102 | <P> | |
103 | This demo shows how individual radio buttons can be used to build | |
104 | more complicated selection mechanisms... | |
105 | <P> | |
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. | |
110 | </body></html> | |
111 | """ | |
112 | ||
113 | ||
114 | ||
115 | if __name__ == '__main__': | |
116 | import sys,os | |
117 | import run | |
118 | run.main(['', os.path.basename(sys.argv[0])]) | |
119 |