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(var
):
24 Radiobutton_button
= Menubutton(mBar
, text
='Political Party',
26 Radiobutton_button
.pack(side
=LEFT
, padx
='2m')
28 # the primary pulldown
29 Radiobutton_button
.menu
= Menu(Radiobutton_button
)
31 Radiobutton_button
.menu
.add_radiobutton(label
='Republican',
32 variable
=var
, value
=1)
34 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Democrat',
38 Radiobutton_button
.menu
.add('radiobutton', {'label': 'Libertarian',
44 # set up a pointer from the file menubutton back to the file menu
45 Radiobutton_button
['menu'] = Radiobutton_button
.menu
47 return Radiobutton_button
52 Radiobutton_button
= Menubutton(mBar
, text
='Flavors',
54 Radiobutton_button
.pack(side
=LEFT
, padx
='2m')
56 # the primary pulldown
57 Radiobutton_button
.menu
= Menu(Radiobutton_button
)
59 Radiobutton_button
.menu
.add_radiobutton(label
='Strawberry',
60 variable
=var
, value
='Strawberry')
62 Radiobutton_button
.menu
.add_radiobutton(label
='Chocolate',
63 variable
=var
, value
='Chocolate')
65 Radiobutton_button
.menu
.add_radiobutton(label
='Rocky Road',
66 variable
=var
, value
='Rocky Road')
71 # set up a pointer from the file menubutton back to the file menu
72 Radiobutton_button
['menu'] = Radiobutton_button
.menu
74 return Radiobutton_button
78 print "party is", party
.get()
79 print "flavor is", flavor
.get()
82 #################################################
83 #### Main starts here ...
88 mBar
= Frame(root
, relief
=RAISED
, borderwidth
=2)
91 # make two application variables,
92 # one to control each radio button set
96 Radiobutton_button
= makePoliticalParties(party
)
97 Radiobutton_button2
= makeFlavors(flavor
)
99 # finally, install the buttons in the menu bar.
100 # This allows for scanning from one menubutton to the next.
101 mBar
.tk_menuBar(Radiobutton_button
, Radiobutton_button2
)
103 b
= Button(root
, text
="print party and flavor", foreground
="red",
107 root
.title('menu demo')
108 root
.iconname('menu demo')