'Menu bars of Tkinter in Mac aren't working
I am trying to learn tkinter and am on the menubars part. I am creating a simple GUI window and trying to display the menu bar. But it is not displaying there. In manY places they say that the menu bar will be showing in the mac one and not in the window itself but mine is not showing there also. Here is the code I am using
from tkinter import *
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
mainmenu = Menu(root)
mainmenu.add_command(label="hello", command=quit)
mainmenu.add_command(label="exit", command=lambda:print("hello"))
root.config(menu=mainmenu)
root.mainloop()
I tried to create a window with a menubar with this code:
from tkinter import *
root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)
mainmenu = Menu(root)
mainmenu.add_command(label="hello", command=quit)
mainmenu.add_command(label="exit", command=lambda:print("hello"))
root.config(menu=mainmenu)
root.mainloop()
Instead it resulted a blank window with no menubar even in the mac one.
Solution 1:[1]
OSX doesn't allow you to put commands directly on the main menu. On the main menu you can only add cascades. This is subtly mentioned in the official documentation:
On the Macintosh, whenever the toplevel is in front, this menu's cascade items will appear in the menubar across the top of the main monitor. On Windows and Unix, this menu's items will be displayed in a menubar across the top of the window.
Notice how it only mentions cascade items for the Mac, but all items for the other platforms.
mainmenu = Menu(root)
root.config(menu=mainmenu)
filemenu = Menu(mainmenu)
mainmenu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="hello", command=quit)
filemenu.add_command(label="exit", command=lambda:print("hello"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bryan Oakley |
