• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from 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
22def makePoliticalParties(var):
23    # make menu button
24    Radiobutton_button = Menubutton(mBar, text='Political Party',
25                                    underline=0)
26    Radiobutton_button.pack(side=LEFT, padx='2m')
27
28    # the primary pulldown
29    Radiobutton_button.menu = Menu(Radiobutton_button)
30
31    Radiobutton_button.menu.add_radiobutton(label='Republican',
32                                            variable=var, value=1)
33
34    Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat',
35                                                'variable' : var,
36                                                'value' : 2})
37
38    Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian',
39                                                'variable' : var,
40                                                'value' : 3})
41
42    var.set(2)
43
44    # set up a pointer from the file menubutton back to the file menu
45    Radiobutton_button['menu'] = Radiobutton_button.menu
46
47    return Radiobutton_button
48
49
50def makeFlavors(var):
51    # make menu button
52    Radiobutton_button = Menubutton(mBar, text='Flavors',
53                                    underline=0)
54    Radiobutton_button.pack(side=LEFT, padx='2m')
55
56    # the primary pulldown
57    Radiobutton_button.menu = Menu(Radiobutton_button)
58
59    Radiobutton_button.menu.add_radiobutton(label='Strawberry',
60                                            variable=var, value='Strawberry')
61
62    Radiobutton_button.menu.add_radiobutton(label='Chocolate',
63                                            variable=var, value='Chocolate')
64
65    Radiobutton_button.menu.add_radiobutton(label='Rocky Road',
66                                            variable=var, value='Rocky Road')
67
68    # choose a default
69    var.set("Chocolate")
70
71    # set up a pointer from the file menubutton back to the file menu
72    Radiobutton_button['menu'] = Radiobutton_button.menu
73
74    return Radiobutton_button
75
76
77def printStuff():
78    print "party is", party.get()
79    print "flavor is", flavor.get()
80    print
81
82#################################################
83#### Main starts here ...
84root = Tk()
85
86
87# make a menu bar
88mBar = Frame(root, relief=RAISED, borderwidth=2)
89mBar.pack(fill=X)
90
91# make two application variables,
92# one to control each radio button set
93party = IntVar()
94flavor = StringVar()
95
96Radiobutton_button = makePoliticalParties(party)
97Radiobutton_button2 = makeFlavors(flavor)
98
99# finally, install the buttons in the menu bar.
100# This allows for scanning from one menubutton to the next.
101mBar.tk_menuBar(Radiobutton_button, Radiobutton_button2)
102
103b = Button(root, text="print party and flavor", foreground="red",
104           command=printStuff)
105b.pack(side=TOP)
106
107root.title('menu demo')
108root.iconname('menu demo')
109
110root.mainloop()
111