Guido van Rossum | 38862df | 1995-02-13 14:23:38 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | |
| 3 | # The way to think about this is that each radio button menu |
| 4 | # controls a different variable -- clicking on one of the |
| 5 | # mutually exclusive choices in a radiobutton assigns some value |
| 6 | # to an application variable you provide. When you define a |
| 7 | # radiobutton menu choice, you have the option of specifying the |
| 8 | # name of a varaible and value to assign to that variable when |
| 9 | # that choice is selected. This clever mechanism relieves you, |
| 10 | # the programmer, from having to write a dumb callback that |
| 11 | # probably wouldn't have done anything more than an assignment |
| 12 | # anyway. The Tkinter options for this follow their Tk |
| 13 | # counterparts: |
| 14 | # {"variable" : my_flavor_variable, "value" : "strawberry"} |
| 15 | # where my_flavor_variable is an instance of one of the |
| 16 | # subclasses of Variable, provided in Tkinter.py (there is |
| 17 | # StringVar(), IntVar(), DoubleVar() and BooleanVar() to choose |
| 18 | # from) |
| 19 | |
| 20 | |
| 21 | |
| 22 | def makePoliticalParties(): |
| 23 | # make menu button |
| 24 | Radiobutton_button = Menubutton(mBar, {'text': 'Political Party', |
| 25 | 'underline': 0, |
| 26 | Pack: {'side': 'left', |
| 27 | 'padx': '2m'}}) |
| 28 | |
| 29 | # the primary pulldown |
| 30 | Radiobutton_button.menu = Menu(Radiobutton_button) |
| 31 | |
| 32 | Radiobutton_button.menu.add('radiobutton', {'label': 'Republican', |
| 33 | 'variable' : party, |
| 34 | 'value' : 1}) |
| 35 | |
| 36 | Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat', |
| 37 | 'variable' : party, |
| 38 | 'value' : 2}) |
| 39 | |
| 40 | Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian', |
| 41 | 'variable' : party, |
| 42 | 'value' : 3}) |
| 43 | |
| 44 | party.set(2) |
| 45 | |
| 46 | # set up a pointer from the file menubutton back to the file menu |
| 47 | Radiobutton_button['menu'] = Radiobutton_button.menu |
| 48 | |
| 49 | return Radiobutton_button |
| 50 | |
| 51 | |
| 52 | def makeFlavors(): |
| 53 | # make menu button |
| 54 | Radiobutton_button = Menubutton(mBar, {'text': 'Flavors', |
| 55 | 'underline': 0, |
| 56 | Pack: {'side': 'left', |
| 57 | 'padx': '2m'}}) |
| 58 | # the primary pulldown |
| 59 | Radiobutton_button.menu = Menu(Radiobutton_button) |
| 60 | |
| 61 | Radiobutton_button.menu.add('radiobutton', {'label': 'Strawberry', |
| 62 | 'variable' : flavor, |
| 63 | 'value' : 'Strawberry'}) |
| 64 | |
| 65 | Radiobutton_button.menu.add('radiobutton', {'label': 'Chocolate', |
| 66 | 'variable' : flavor, |
| 67 | 'value' : 'Chocolate'}) |
| 68 | |
| 69 | Radiobutton_button.menu.add('radiobutton', {'label': 'Rocky Road', |
| 70 | 'variable' : flavor, |
| 71 | 'value' : 'Rocky Road'}) |
| 72 | |
| 73 | # choose a default |
| 74 | flavor.set("Chocolate") |
| 75 | |
| 76 | # set up a pointer from the file menubutton back to the file menu |
| 77 | Radiobutton_button['menu'] = Radiobutton_button.menu |
| 78 | |
| 79 | return Radiobutton_button |
| 80 | |
| 81 | |
| 82 | def printStuff(): |
| 83 | print "party is", party.get() |
| 84 | print "flavor is", flavor.get() |
| 85 | print "" |
| 86 | |
| 87 | ################################################# |
| 88 | #### Main starts here ... |
| 89 | root = Tk() |
| 90 | |
| 91 | |
| 92 | # make a menu bar |
| 93 | mBar = Frame(root, {'relief': 'raised', |
| 94 | 'bd': 2, |
| 95 | Pack: {'side': 'top', |
| 96 | 'fill': 'x'}}) |
| 97 | |
| 98 | # make two application variables, |
| 99 | # one to control each radio button set |
| 100 | party = IntVar() |
| 101 | flavor = StringVar() |
| 102 | |
| 103 | Radiobutton_button = makePoliticalParties() |
| 104 | Radiobutton_button2 = makeFlavors() |
| 105 | |
| 106 | # finally, install the buttons in the menu bar. |
| 107 | # This allows for scanning from one menubutton to the next. |
| 108 | mBar.tk_menuBar(Radiobutton_button, Radiobutton_button2) |
| 109 | |
| 110 | b = Button(root, {"text": "print party and flavor", |
| 111 | "command" : printStuff, |
| 112 | "fg": "red"}) |
| 113 | b.pack({"side" : "top"}) |
| 114 | |
| 115 | root.title('menu demo') |
| 116 | root.iconname('menu demo') |
| 117 | |
| 118 | root.mainloop() |