在之前的blog中有提到python的tkinter中的菜单操作
下面是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
9 def makeCascadeMenu():
10 # make menu button
11 Cascade_button = Menubutton(mBar, text='Cascading Menus', underline=0)
12 Cascade_button.pack(side=LEFT, padx="2m")
13
14 # the primary pulldown
15 Cascade_button.menu = Menu(Cascade_button)
16
17 # this is the menu that cascades from the primary pulldown....
18 Cascade_button.menu.choices = Menu(Cascade_button.menu)
19
20 # ...and this is a menu that cascades from that.
21 Cascade_button.menu.choices.weirdones = Menu(Cascade_button.menu.choices)
22
23 # then you define the menus from the deepest level on up.
24 Cascade_button.menu.choices.weirdones.add_command(label='avacado', command=lambda:print('hello'))
25 Cascade_button.menu.choices.weirdones.add_command(label='belgian endive')
26 Cascade_button.menu.choices.weirdones.add_command(label='beefaroni')
27
28 # definition of the menu one level up...
29 Cascade_button.menu.choices.add_command(label='Chocolate')
30 Cascade_button.menu.choices.add_command(label='Vanilla')
31 Cascade_button.menu.choices.add_command(label='TuttiFruiti')
32 Cascade_button.menu.choices.add_command(label='WopBopaLoopBapABopBamBoom')
33 Cascade_button.menu.choices.add_command(label='Rocky Road')
34 Cascade_button.menu.choices.add_command(label='BubbleGum')
35 Cascade_button.menu.choices.add_cascade(
36 label='Weird Flavors',
37 menu=Cascade_button.menu.choices.weirdones)
38
39 # and finally, the definition for the top level
40 Cascade_button.menu.add_cascade(label='more choices',
41 menu=Cascade_button.menu.choices)
42
43 Cascade_button['menu'] = Cascade_button.menu
44
45 return Cascade_button
46
47
48 #################################################
49 #### Main starts here ...
50 root = Tk()
51 root.geometry('600x300')
52
53
54 # make a menu bar
55 mBar = Frame(root, relief=RAISED, borderwidth=2)
56 mBar.pack(fill=X)
57
58
59 Cascade_button = makeCascadeMenu()
60
61 mBar.tk_menuBar(Cascade_button)
62
63
64 root.title('menu demo')
65 root.iconname('menu demo')
66
67 root.mainloop()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有