python开发_tkinter_单选菜单_不可用菜单操作

在之前的blog中有提到python的tkinter中的菜单操作

python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐

python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)

python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐

python开发_tkinter_复选菜单

下面是单选菜单和不可用菜单操作

运行效果

============================================================

代码部分:

============================================================

 1 from tkinter import *
 2 
 3 __author__ = {'name' : 'Hongten',
 4               'mail' : 'hongtenzone@foxmail.com',
 5               'blog' : 'http://www.cnblogs.com/',
 6               'QQ': '648719819',
 7               'created' : '2013-09-10'}
 8 #global var
 9 checked_box = [False, False, False, False, False, False, False, False, False, False]
10 radio_items = ['Republican', 'Democrat', 'Libertarian', 'Commie',
11                'Facist', 'Labor Party', 'Torie', 'Independent',
12                'Anarchist', 'No Opinion']
13 opinion = False
14 #################################################
15 #### Main starts here ...
16 root = Tk()
17 root.geometry('250x400')
18 root.title('menu demo')
19 root.iconname('menu demo')
20 
21 def print_info(index):
22     global checked_box
23     global radio_items
24     n = [x for x in range(0, len(checked_box))]
25     if index in n:
26         checked_box[index - 1] = not checked_box[index - 1]
27         print(radio_items[index - 1], '?', checked_box[index - 1])
28     else:
29         pass
30     
31 def print_opinion():
32     global opinion
33     opinion = not opinion
34     print('opinion ? ', opinion)
35 
36 def makeRadiobuttonMenu():
37     # make menu button
38     Radiobutton_button = Menubutton(mBar, text='Radiobutton Menus',
39                                     underline=0)
40     Radiobutton_button.pack(side=LEFT, padx='2m')
41 
42     # the primary pulldown
43     Radiobutton_button.menu = Menu(Radiobutton_button)
44 
45     # and all the Radio buttons. Note that the "variable" "onvalue" and "offvalue" options
46     # are not supported correctly at present. You have to do all your application
47     # work through the calback.
48     Radiobutton_button.menu.add_radiobutton(label='Republican', command=print_info(1))
49     Radiobutton_button.menu.add_radiobutton(label='Democrat', command=print_info(2))
50     Radiobutton_button.menu.add_radiobutton(label='Libertarian', command=print_info(3))
51     Radiobutton_button.menu.add_radiobutton(label='Commie', command=print_info(4))
52     Radiobutton_button.menu.add_radiobutton(label='Facist', command=print_info(5))
53     Radiobutton_button.menu.add_radiobutton(label='Labor Party', command=print_info(6))
54     Radiobutton_button.menu.add_radiobutton(label='Torie', command=print_info(7))
55     Radiobutton_button.menu.add_radiobutton(label='Independent', command=print_info(8))
56     Radiobutton_button.menu.add_radiobutton(label='Anarchist', command=print_info(9))
57     Radiobutton_button.menu.add_radiobutton(label='No Opinion', command=print_opinion())
58 
59     # set up a pointer from the file menubutton back to the file menu
60     Radiobutton_button['menu'] = Radiobutton_button.menu
61 
62     return Radiobutton_button
63 
64 
65 def makeDisabledMenu():
66     Dummy_button = Menubutton(mBar, text='Dead Menu', underline=0)
67     Dummy_button.pack(side=LEFT, padx='2m')
68 
69     # this is the standard way of turning off a whole menu
70     Dummy_button["state"] = DISABLED
71     return Dummy_button
72 
73 # make a menu bar
74 mBar = Frame(root, relief=RAISED, borderwidth=2)
75 mBar.pack(fill=X)
76 
77 Radiobutton_button = makeRadiobuttonMenu()
78 NoMenu = makeDisabledMenu()
79 
80 mBar.tk_menuBar(Radiobutton_button, NoMenu)
81 
82 root.mainloop()

 

 

posted @ 2013-09-10 18:40  Hongten  阅读(3150)  评论(0编辑  收藏  举报
Fork me on GitHub