Use py_resource module
[python/dscho.git] / Demo / tkinter / matt / two-radio-groups.py
blobf65c8a9c493777601553e8cbcc63224a6eb2e9bc
1 from Tkinter import *
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)
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'}})
29 # the primary pulldown
30 Radiobutton_button.menu = Menu(Radiobutton_button)
32 Radiobutton_button.menu.add('radiobutton', {'label': 'Republican',
33 'variable' : party,
34 'value' : 1})
36 Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat',
37 'variable' : party,
38 'value' : 2})
40 Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian',
41 'variable' : party,
42 'value' : 3})
44 party.set(2)
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
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)
61 Radiobutton_button.menu.add('radiobutton', {'label': 'Strawberry',
62 'variable' : flavor,
63 'value' : 'Strawberry'})
65 Radiobutton_button.menu.add('radiobutton', {'label': 'Chocolate',
66 'variable' : flavor,
67 'value' : 'Chocolate'})
69 Radiobutton_button.menu.add('radiobutton', {'label': 'Rocky Road',
70 'variable' : flavor,
71 'value' : 'Rocky Road'})
73 # choose a default
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
82 def printStuff():
83 print "party is", party.get()
84 print "flavor is", flavor.get()
85 print ""
87 #################################################
88 #### Main starts here ...
89 root = Tk()
92 # make a menu bar
93 mBar = Frame(root, {'relief': 'raised',
94 'bd': 2,
95 Pack: {'side': 'top',
96 'fill': 'x'}})
98 # make two application variables,
99 # one to control each radio button set
100 party = IntVar()
101 flavor = StringVar()
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,
112 "fg": "red"})
113 b.pack({"side" : "top"})
115 root.title('menu demo')
116 root.iconname('menu demo')
118 root.mainloop()