When I run the code below, Pyton ignores all references to Menu and produces a form with a single input box. It seems to load the window but none of the Menu items. Here is an example taken from the Python tutorial:
from tkinter import *
from tkinter import ttk, messagebox
root = Tk()
ttk.Entry(root).grid()
m = Menu(root)
m_edit = Menu(m)
m.add_cascade(menu=m_edit, label="Edit")
m_edit.add_command(label="Paste", command=lambda: root.focus_get().event_generate("<<Paste>>"))
m_edit.add_command(label="Find...", command=lambda: root.event_generate("<<OpenFindDialog>>"))
root['menu'] = m
def launchFindDialog(*args):
messagebox.showinfo(message="I hope you find what you're looking for!")
root.bind("<<OpenFindDialog>>", launchFindDialog)
root.mainloop()
One macos, the menu gets placed in the menu bar at the top. You haven’t shown that it isn’t there in your screenshot.
IDLE is built ontop of tkinter, so since you are using it, the python-tkinter interaction works and it’s either a problem in your code, or, in this case I think, your expectations of what your code should be doing.
I expected to see a Menu showing “Edit” cascading to Paste etc. Am I wrong to expect this? The code is copied and pasted from the Python official documentation. It shouldn’t have any errors. Maybe it is because I am running on a Mac Mini M1 chip???
Would you mind showing me the resultant window after you ran the code so that I can see what to expect? I am a beginner in Python programming, having only programmed in MS Visual Basic a long time ago.
Yes, this is the exact behavior that you will not see on macos. The menu will be put into the top bar, i.e. top of the desktop, as is standard for all macos apps. I am not on a macos, so I can’t share a screenshot.
Thank you Cornelius. You are 100% correct. I was looking for the menu in the window. I now see that it is in the top bar as you have pointed out. Apologies for wasting everyone’s time. I had no idea that this was classic mac behaviour though I have been using a Mac since 2012.
Yes, thanks Terry. I should have realised all this with my long period of use of canned programs on my Mac, but I’ve never programmed anything on a Mac before; only on a PC. It’s a case of not seeing the elephant in the room because it has always been there!
It might also be useful to note that there’s some other special behaviours, activated by giving sub-menus special names. You can set name="apple", window or help to add your items into those default menus.
Hi Spencer. I’ve just got back to this problem and, even after looking at the blurb in your link, it’s not clear to me how to get the “apple” part of it into the syntax. I don’t find that part of the Tk manual easy to follow.
If you take the menu example I gave above, what would this extra line look like and where would I put it?
You’d just need to change the second menu construction to be Menu(m, name="apple"). All Tk widgets fundamentally have a name, which is composed of it and all its parents. The root Tk() widget is named ., then others get named like .!frame5.!menu3.!menu8 by default. You can override the auto-name with the name option.