问题
I'm trying to add a menu into the UI but it doesn't show up. I copied everything directly from effbot.org/tkinterbook/menu.htm but it still isn't working. I'm using Python 3 and macOS 10 if that's relevant.
from tkinter import *
master = Tk()
master.geometry('300x300')
master.title('Table')
def hello():
print("hello")
menubar = Menu(master)
menubar.add_command(label = "Hello!", command = hello)
menubar.add_command(label = "Quit!", command = master.quit)
master.config(menu=menubar)
master.mainloop()
Edit: I tried the submenu but it still does not appear and the window is empty: code and tkinter window
回答1:
On OSX, you cannot put commands on the menubar. Also, you showed a screenshot of the window but on OSX the menubar appears at the top of the screen just like a normal Mac app.
回答2:
I have tested your code and it appears to work just fine on my end.
Python 3.6 / Windows 10.
Here is an image showing the code in question working fine.
Like Bryan had mentioned maybe you need to be using a submenu here.
Revised code:
from tkinter import *
master = Tk()
master.geometry('300x300')
master.title('Table')
def hello():
print("hello")
menubar = Menu(master)
master.config(menu=menubar)
submenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=submenu)
submenu.add_command(label="Hello!", command=hello)
submenu.add_command(label="Quit!", command=master.quit)
master.mainloop()
来源:https://stackoverflow.com/questions/57560824/why-is-the-menu-not-appearing-in-the-window