I was following a tutorial and received different results when doing exactly what was said.
I want the menu bar to show up inside the actual tkinter application window like it does on the tutorial. He is using Windows. It is instead showing up on the actual top menu bar of the computer. I am using a Mac if that makes a difference. Is there a way to fix this?
This is my code:
from tkinter import *
def doNothing():
label1 = Label(root, text="Doing nothing")
label1.pack()
root = Tk()
mainmenu = Menu(root)
root.config(menu=mainmenu)
filemenu = Menu(mainmenu)
mainmenu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New Project", command=doNothing)
filemenu.add_command(label="New", command=doNothing)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=doNothing)
editmenu=Menu(mainmenu)
mainmenu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Redo", command=doNothing)
root.mainloop()
Press the Alt key to temporarily show the Menu bar. (If the Alt key doesn't work, press the AltGr key.)
I am using a Mac if that makes a difference. Is there a way to fix this?
Yes, it makes a difference. Tk
still doesn't support styling menus. According to TkDocs
:
You'll notice on some recent Linux distributions that many applications show their menus at the top of the screen when active, rather than in the window itself. Tk does not yet support this style of menus.
And further below it says:
On Mac OS X though, there is a single menubar along the top of the screen, shared by each window. As far as your Tk program is concerned, each window still does have its own menubar; as you switch between windows, Tk will automatically take care of making sure that the correct menubar is displayed at the top of the screen. If you don't specify a menubar for a particular window, Tk will use the menubar associated with the root window; you'll have noticed by now that this is automatically created for you when your Tk application starts.
and then it gives us a tip:
Because on Mac OS X all windows have a menubar, it's important to make sure you do define one, either for each window or a fallback menubar for the root window. Otherwise, you'll end up with the "built-in" menubar, which contains menus that are only intended for use when typing commands directly into the interpreter.
Furthermore, this issue says:
The Tk menu command is hooked into the Mac OS X global menubar. If you want a menu without using the global menubar, you will have to write your own top level menu.
And here's a person asking how to possible remove the menu at the top, in case you're interested in implementing your menu on the window itself:
The name that shows up in the menu is derived by OS X from the application name in the executing application bundle. If you don't package your program up as an OS X application bundle, defaults will be used; in the case of Python OS X framework builds, Python provides a Python.app within the framework to allow the Python process to be automatically promoted to a full OS X GUI process.
Probably the simplest approach is to usepy2app
to create a double-clickable app with the name you want. There's an example in an answer to a similar question on Stackoverflow. And there are some old but still relevant details documented in the Tcl/TkAqua FAQ.
And in general, according to this article
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 accross the top of the window. These menus will behave according to the interface guidelines of their platforms
...
As noted, menubars may behave differently on different platforms. One example of this concerns the handling of checkbuttons and radiobuttons within the menu. While it is permitted to put these menu elements on menubars, they may not be drawn with indicators on some platforms, due to system restrictions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With