Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

python - Creating sub-menus inside sub-menu in Tkinter

I recently started learning about tkinter menu's.

Is there any way to add sub-menus inside a sub-menu ? I mean , when I hover over a option in my menu , I want more sub-menus to show up. I'm very sorry if I am unclear , I don't know how do I explain this , but here's an image(GIF) of what I want .is there any way to achieve this in tkinter ?

Here's the code

from tkinter import *
root = Tk()
root.geometry("500x500")
mainmenu = Menu(root)
root.config(menu = mainmenu)
test_menu = Menu(mainmenu , tearoff = False)
mainmenu.add_cascade(label = "Test Menu" , menu = test_menu)
test_menu.add_command(label = "Show Sub-menus           ")
mainloop()

It would be great if anyone could help me out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can just keep going adding sub menus using add_cascade:

sub_menu = Menu(test_menu, tearoff=False)
sub_menu.add_command(label='Submenu item 1')
sub_menu.add_command(label='Submenu item 2')

test_menu.add_cascade(label = 'Sub menu', menu=sub_menu)

Tkinter window with a sub menu

sub_sub_menu = Menu(sub_menu, tearoff=False)
sub_sub_menu.add_command(label='Sub-submenu item 1')
sub_sub_menu.add_command(label='Sub-submenu item 2')

sub_menu.add_cascade(label='Sub-sub menu', menu=sub_sub_menu)

Tkinter window with a sub-sub menu


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...