python开发_tkinter_复选菜单

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

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

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

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

下面是tkinter中复选菜单的操作

运行效果:

1.初始化的时候,最后一个子菜单被选中。

2.选择子菜单项,所触发的事件...

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

代码部分:

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

 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 #状态标志
 9 pepperonis = False
10 anchovies = 0
11 
12 def print_pepperonis():
13     global pepperonis
14     pepperonis = not pepperonis
15     print('pepperonis?', pepperonis)
16 
17 def print_anchovies():
18     '''从这里我们可以判断出'Anchovy'子菜单是否处于选择状态'''
19     global anchovies
20     anchovies = not anchovies
21     print("anchovies?", anchovies)
22 
23 def makeCheckbuttonMenu():
24     # make menu button
25     Checkbutton_button = Menubutton(mBar, text='Checkbutton Menus',
26                                     underline=0)
27     Checkbutton_button.pack(side=LEFT, padx='2m')
28 
29     # the primary pulldown
30     Checkbutton_button.menu = Menu(Checkbutton_button)
31 
32     # and all the check buttons. Note that the "variable" "onvalue" and "offvalue" options
33     # are not supported correctly at present. You have to do all your application
34     # work through the calback.
35     Checkbutton_button.menu.add_checkbutton(label='Pepperoni', command=print_pepperonis)
36     Checkbutton_button.menu.add_checkbutton(label='Sausage')
37     Checkbutton_button.menu.add_checkbutton(label='Extra Cheese')
38 
39     # so here's a callback
40     Checkbutton_button.menu.add_checkbutton(label='Anchovy',
41                                             command=print_anchovies)
42     #初始化时,被选中状态
43     #
44     # and start with anchovies selected to be on. Do this by
45     # calling invoke on this menu option. To refer to the "anchovy" menu
46     # entry we need to know it's index. To do this, we use the index method
47     # which takes arguments of several forms:
48     #
49     # argument        what it does
50     # -----------------------------------
51     # a number        -- this is useless.
52     # "last"          -- last option in the menu
53     # "none"          -- used with the activate command. see the man page on menus
54     # "active"        -- the currently active menu option. A menu option is made active
55     #                         with the 'activate' method
56     # "@number"       -- where 'number' is an integer and is treated like a y coordinate in pixels
57     # string pattern  -- this is the option used below, and attempts to match "labels" using the
58     #                    rules of Tcl_StringMatch
59     Checkbutton_button.menu.invoke(Checkbutton_button.menu.index('Anchovy'))
60 
61     # set up a pointer from the file menubutton back to the file menu
62     Checkbutton_button['menu'] = Checkbutton_button.menu
63 
64     return Checkbutton_button
65 
66 
67 #################################################
68 #### Main starts here ...
69 root = Tk()
70 root.geometry('250x200')
71 root.title('menu demo')
72 root.iconname('menu demo')
73 
74 # make a menu bar
75 mBar = Frame(root, relief=RAISED, borderwidth=2)
76 mBar.pack(fill=X)
77 
78 Checkbutton_button = makeCheckbuttonMenu()
79 
80 mBar.tk_menuBar(Checkbutton_button)
81 
82 root.mainloop()

 

 

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