菜单栏未出现在 Tkinter GUI 上
Menu Bar not appearing on Tkinter GUI
我是 Tkinter 的新手,目前正在从视频中学习,我正在尝试创建一个菜单栏,其中包含一个带有一些选项和分隔符的进一步下拉菜单,它们没有任何功能,因为我我只是想在添加之前实现该目标。
这是代码:
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()
当我 运行 代码时,我得到一个空白 window,我正在从一个显示代码工作的视频中复制。
This 是我正在看的视频。我试过将它放在 class 中,但它仍然不起作用。
尝试使用此代码创建简单的下拉菜单
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()
您可以进一步向菜单中的每个选项添加命令
这些主要是参考错误。
第 14 行:您引用了一个不存在的名称:
menuTop.add_cascade(label='File', menu=subMenu)
# Should be ----------------------menu=submenuDown
第 17 行:拼写错误:
submenuDown.add_seperator()
# Should be -------a
第 21 行:引用错误:
menu.add_cascade(label='Edit', menu=editMenu)
menuTop ------- should be
经过这些更改后它运行良好:
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()
我是 Tkinter 的新手,目前正在从视频中学习,我正在尝试创建一个菜单栏,其中包含一个带有一些选项和分隔符的进一步下拉菜单,它们没有任何功能,因为我我只是想在添加之前实现该目标。 这是代码:
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()
当我 运行 代码时,我得到一个空白 window,我正在从一个显示代码工作的视频中复制。 This 是我正在看的视频。我试过将它放在 class 中,但它仍然不起作用。
尝试使用此代码创建简单的下拉菜单
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()
您可以进一步向菜单中的每个选项添加命令
这些主要是参考错误。
第 14 行:您引用了一个不存在的名称:
menuTop.add_cascade(label='File', menu=subMenu)
# Should be ----------------------menu=submenuDown
第 17 行:拼写错误:
submenuDown.add_seperator()
# Should be -------a
第 21 行:引用错误:
menu.add_cascade(label='Edit', menu=editMenu)
menuTop ------- should be
经过这些更改后它运行良好:
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()