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
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
22 def makePoliticalParties():
24 Radiobutton_button
= Menubutton(mBar
, {'text': 'Political Party',
26 Pack
: {'side': 'left',
29 # the primary pulldown
30 Radiobutton_button
.menu
= Menu(Radiobutton_button
)
32 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Republican',
36 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Democrat',
40 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Libertarian',
46 # set up a pointer from the file menubutton back to the file menu
47 Radiobutton_button
['menu'] = Radiobutton_button
.menu
49 return Radiobutton_button
54 Radiobutton_button
= Menubutton(mBar
, {'text': 'Flavors',
56 Pack
: {'side': 'left',
58 # the primary pulldown
59 Radiobutton_button
.menu
= Menu(Radiobutton_button
)
61 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Strawberry',
63 'value' : 'Strawberry'})
65 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Chocolate',
67 'value' : 'Chocolate'})
69 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Rocky Road',
71 'value' : 'Rocky Road'})
74 flavor
.set("Chocolate")
76 # set up a pointer from the file menubutton back to the file menu
77 Radiobutton_button
['menu'] = Radiobutton_button
.menu
79 return Radiobutton_button
83 print "party is", party
.get()
84 print "flavor is", flavor
.get()
87 #################################################
88 #### Main starts here ...
93 mBar
= Frame(root
, {'relief': 'raised',
98 # make two application variables,
99 # one to control each radio button set
103 Radiobutton_button
= makePoliticalParties()
104 Radiobutton_button2
= makeFlavors()
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
)
110 b
= Button(root
, {"text": "print party and flavor",
111 "command" : printStuff
,
113 b
.pack({"side" : "top"})
115 root
.title('menu demo')
116 root
.iconname('menu demo')