Reputation: 15
I'm new to Tkinter and currently am learning from videos, I'm trying to create a menu bar with a further drop-down menu with some options and a separator, they won't have any function as I'm just trying to achieve that goal before adding onto it. Here's the code:
from tkinter import *
def donothing():
print('Done')
root = Tk()
menuTop = Menu(root) # this is a blank menu bar at the top
root.config(menu=menuTop) # configures the menu feature, and assigns it to the Menu value
submenuDown = Menu(menuTop) # this is a meta menu that *will* drop down
menuTop.add_cascade(label='File', menu=subMenu) # assigns drop down menu to submenu def
submenuDown.add_command(label='Yes...', command=donothing)
submenuDown.add_command(label='No...', command=donothing)
submenuDown.add_seperator()
submenuDown.add_command(label='Exit!', command=donothing)
editMenu = Menu(menuTop)
menu.add_cascade(label='Edit', menu=editMenu)
editMenu.add_command(label='Redo', command=donothing)
root.mainloop()
When I run the code I get a blank window, I'm copying from a video which shows the code working as shown. This is the video I am watching. I've tried putting it in a class but it still doesn't work.
Upvotes: 1
Views: 680
Reputation: 7176
These are mostly reference errors.
Line 14: you have referenced a name which does not exist:
menuTop.add_cascade(label='File', menu=subMenu)
# Should be ----------------------menu=submenuDown
Line 17: spelling error:
submenuDown.add_seperator()
# Should be -------a
Line 21: reference error:
menu.add_cascade(label='Edit', menu=editMenu)
menuTop ------- should be
After these changes it runs fine:
from tkinter import *
def donothing():
print('Done')
root = Tk()
menuTop = Menu(root) # this is a blank menu bar at the top
root.config(menu=menuTop) # configures the menu feature, and assigns it to the Menu value
submenuDown = Menu(menuTop) # this is a meta menu that *will* drop down
menuTop.add_cascade(label='File', menu=submenuDown) # assigns drop down menu to submenu def
submenuDown.add_command(label='Yes...', command=donothing)
submenuDown.add_command(label='No...', command=donothing)
submenuDown.add_separator()
submenuDown.add_command(label='Exit!', command=donothing)
editMenu = Menu(menuTop)
menuTop.add_cascade(label='Edit', menu=editMenu)
editMenu.add_command(label='Redo', command=donothing)
root.mainloop()
Upvotes: 2
Reputation: 154
Try this code for a simple drop down menu
from tkinter import *
root=Tk()
root.title('Drop down menu')
root.geometry('400x400')
def show():
mylbl= Label(root,text=clicked.get()).pack()
options =[
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'satarday',
'sunday'
]
clicked =StringVar()
drop =OptionMenu(root,clicked,*options).pack()
root.mainloop()
You can further add commands to each option in the menu
Upvotes: 2