在之前的blog中有提到python的tkinter中的菜单操作
下面是tkinter的获取单选菜单值的操作
运行效果:
当点击'print party and flavor'按钮的时候,获取单选菜单的值
==========================================================
代码部分:
==========================================================
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 __author__ = {'name' : 'Hongten',
21 'mail' : 'hongtenzone@foxmail.com',
22 'blog' : 'http://www.cnblogs.com/',
23 'QQ': '648719819',
24 'created' : '2013-09-11'}
25
26 def makePoliticalParties(var):
27 # make menu button
28 Radiobutton_button = Menubutton(mBar, text='Political Party',
29 underline=0)
30 Radiobutton_button.pack(side=LEFT, padx='2m')
31
32 # the primary pulldown
33 Radiobutton_button.menu = Menu(Radiobutton_button)
34
35 Radiobutton_button.menu.add_radiobutton(label='Republican',
36 variable=var, value=1)
37
38 Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat',
39 'variable' : var,
40 'value' : 2})
41
42 Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian',
43 'variable' : var,
44 'value' : 3})
45
46 var.set(2)
47
48 # set up a pointer from the file menubutton back to the file menu
49 Radiobutton_button['menu'] = Radiobutton_button.menu
50
51 return Radiobutton_button
52
53
54 def makeFlavors(var):
55 # make menu button
56 Radiobutton_button = Menubutton(mBar, text='Flavors',
57 underline=0)
58 Radiobutton_button.pack(side=LEFT, padx='2m')
59
60 # the primary pulldown
61 Radiobutton_button.menu = Menu(Radiobutton_button)
62
63 Radiobutton_button.menu.add_radiobutton(label='Strawberry',
64 variable=var, value='Strawberry')
65
66 Radiobutton_button.menu.add_radiobutton(label='Chocolate',
67 variable=var, value='Chocolate')
68
69 Radiobutton_button.menu.add_radiobutton(label='Rocky Road',
70 variable=var, value='Rocky Road')
71
72 # choose a default
73 var.set("Chocolate")
74
75 # set up a pointer from the file menubutton back to the file menu
76 Radiobutton_button['menu'] = Radiobutton_button.menu
77
78 return Radiobutton_button
79
80
81 def printStuff():
82 print("party is", party.get())
83 print("flavor is", flavor.get())
84 print()
85
86 #################################################
87 #### Main starts here ...
88 root = Tk()
89
90
91 # make a menu bar
92 mBar = Frame(root, relief=RAISED, borderwidth=2)
93 mBar.pack(fill=X)
94
95 # make two application variables,
96 # one to control each radio button set
97 party = IntVar()
98 flavor = StringVar()
99
100 Radiobutton_button = makePoliticalParties(party)
101 Radiobutton_button2 = makeFlavors(flavor)
102
103 # finally, install the buttons in the menu bar.
104 # This allows for scanning from one menubutton to the next.
105 mBar.tk_menuBar(Radiobutton_button, Radiobutton_button2)
106
107 b = Button(root, text="print party and flavor", foreground="red",
108 command=printStuff)
109 b.pack(side=TOP)
110
111 root.title('menu demo')
112 root.iconname('menu demo')
113
114 root.mainloop()
参考资料:
http://www.oschina.net/code/explore/Python-3.1.3/Demo/tkinter/matt/two-radio-groups.py