Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 9cf8f33 | 1992-03-30 10:54:51 +0000 | [diff] [blame] | 2 | |
| 3 | # radiogroups.py |
| 4 | # |
| 5 | # Demonstrate multiple groups of radio buttons |
| 6 | |
| 7 | import stdwin |
| 8 | from Buttons import * |
| 9 | from WindowParent import WindowParent, MainLoop |
| 10 | from HVSplit import HSplit, VSplit |
| 11 | |
| 12 | def main(): |
| 13 | # |
| 14 | # Create the widget hierarchy, top-down |
| 15 | # |
| 16 | # 1. Create the window |
| 17 | # |
| 18 | window = WindowParent().create('Radio Groups', (0, 0)) |
| 19 | # |
| 20 | # 2. Create a horizontal split to contain the groups |
| 21 | # |
| 22 | topsplit = HSplit().create(window) |
| 23 | # |
| 24 | # 3. Create vertical splits, one for each group |
| 25 | # |
| 26 | group1 = VSplit().create(topsplit) |
| 27 | group2 = VSplit().create(topsplit) |
| 28 | group3 = VSplit().create(topsplit) |
| 29 | # |
| 30 | # 4. Create individual radio buttons, each in their own split |
| 31 | # |
| 32 | b11 = RadioButton().definetext(group1, 'Group 1 button 1') |
| 33 | b12 = RadioButton().definetext(group1, 'Group 1 button 2') |
| 34 | b13 = RadioButton().definetext(group1, 'Group 1 button 3') |
| 35 | # |
| 36 | b21 = RadioButton().definetext(group2, 'Group 2 button 1') |
| 37 | b22 = RadioButton().definetext(group2, 'Group 2 button 2') |
| 38 | b23 = RadioButton().definetext(group2, 'Group 2 button 3') |
| 39 | # |
| 40 | b31 = RadioButton().definetext(group3, 'Group 3 button 1') |
| 41 | b32 = RadioButton().definetext(group3, 'Group 3 button 2') |
| 42 | b33 = RadioButton().definetext(group3, 'Group 3 button 3') |
| 43 | # |
| 44 | # 5. Define the grouping for the radio buttons. |
| 45 | # Note: this doesn't have to be the same as the |
| 46 | # grouping is splits (although it usually is). |
| 47 | # Also set the 'hook' procedure for each button |
| 48 | # |
| 49 | list1 = [b11, b12, b13] |
| 50 | list2 = [b21, b22, b23] |
| 51 | list3 = [b31, b32, b33] |
| 52 | # |
| 53 | for b in list1: |
| 54 | b.group = list1 |
| 55 | b.on_hook = myhook |
| 56 | for b in list2: |
| 57 | b.group = list2 |
| 58 | b.on_hook = myhook |
| 59 | for b in list3: |
| 60 | b.group = list3 |
| 61 | b.on_hook = myhook |
| 62 | # |
| 63 | # 6. Select a default button in each group |
| 64 | # |
| 65 | b11.select(1) |
| 66 | b22.select(1) |
| 67 | b33.select(1) |
| 68 | # |
| 69 | # 6. Realize the window |
| 70 | # |
| 71 | window.realize() |
| 72 | # |
| 73 | # 7. Dispatch events until the window is closed |
| 74 | # |
| 75 | MainLoop() |
| 76 | # |
| 77 | # 8. Report final selections |
| 78 | # |
| 79 | print 'You selected the following choices:' |
| 80 | if b11.selected: print '1.1' |
| 81 | if b12.selected: print '1.2' |
| 82 | if b13.selected: print '1.3' |
| 83 | if b21.selected: print '2.1' |
| 84 | if b22.selected: print '2.2' |
| 85 | if b23.selected: print '2.3' |
| 86 | if b31.selected: print '3.1' |
| 87 | if b32.selected: print '3.2' |
| 88 | if b33.selected: print '3.3' |
| 89 | |
| 90 | |
| 91 | # My 'hook' procedure |
| 92 | # This is placed as 'hook' attribute on each button. |
| 93 | # The example just prints the title of the selected button. |
| 94 | # |
| 95 | def myhook(self): |
| 96 | print 'Selected:', self.text |
| 97 | |
| 98 | main() |