Python3 not interacting with Tkinter

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()
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)

This is the result when I run this code in IDLE:

Menu test

I would be grateful for any assistance.

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.

On my side, the code works as it is. Although, I am not on Mac.

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.

This is the image, after I clicked on the Edit to see what is inside.


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 Franklinvp. That is what I hoped to see.

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.

If you run IDLE, you will also see its main menu above the window instead of in the window as with *nix and windows.

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.

Thank you Spencer. That is useful to know. I will look at the Tk documentation to learn more.

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.

Thank you very much Spencer. I will do that.