在之前的blog中有提到python的tkinter中的菜单操作
python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐
python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)
python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐
下面是tkinter中复选菜单的操作
运行效果:
1.初始化的时候,最后一个子菜单被选中。
2.选择子菜单项,所触发的事件...
==============================================================
代码部分:
==============================================================
from tkinter import * __author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'blog' : 'http://www.cnblogs.com/',
'QQ': '',
'created' : '2013-09-10'}
#状态标志
pepperonis = False
anchovies = 0 def print_pepperonis():
global pepperonis
pepperonis = not pepperonis
print('pepperonis?', pepperonis) def print_anchovies():
'''从这里我们可以判断出'Anchovy'子菜单是否处于选择状态'''
global anchovies
anchovies = not anchovies
print("anchovies?", anchovies) def makeCheckbuttonMenu():
# make menu button
Checkbutton_button = Menubutton(mBar, text='Checkbutton Menus',
underline=0)
Checkbutton_button.pack(side=LEFT, padx='2m') # the primary pulldown
Checkbutton_button.menu = Menu(Checkbutton_button) # and all the check buttons. Note that the "variable" "onvalue" and "offvalue" options
# are not supported correctly at present. You have to do all your application
# work through the calback.
Checkbutton_button.menu.add_checkbutton(label='Pepperoni', command=print_pepperonis)
Checkbutton_button.menu.add_checkbutton(label='Sausage')
Checkbutton_button.menu.add_checkbutton(label='Extra Cheese') # so here's a callback
Checkbutton_button.menu.add_checkbutton(label='Anchovy',
command=print_anchovies)
#初始化时,被选中状态
#
# and start with anchovies selected to be on. Do this by
# calling invoke on this menu option. To refer to the "anchovy" menu
# entry we need to know it's index. To do this, we use the index method
# which takes arguments of several forms:
#
# argument what it does
# -----------------------------------
# a number -- this is useless.
# "last" -- last option in the menu
# "none" -- used with the activate command. see the man page on menus
# "active" -- the currently active menu option. A menu option is made active
# with the 'activate' method
# "@number" -- where 'number' is an integer and is treated like a y coordinate in pixels
# string pattern -- this is the option used below, and attempts to match "labels" using the
# rules of Tcl_StringMatch
Checkbutton_button.menu.invoke(Checkbutton_button.menu.index('Anchovy')) # set up a pointer from the file menubutton back to the file menu
Checkbutton_button['menu'] = Checkbutton_button.menu return Checkbutton_button #################################################
#### Main starts here ...
root = Tk()
root.geometry('250x200')
root.title('menu demo')
root.iconname('menu demo') # make a menu bar
mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X) Checkbutton_button = makeCheckbuttonMenu() mBar.tk_menuBar(Checkbutton_button) root.mainloop()