Python Tkinter menu bars don't display Python Tkinter menu bars don't display tkinter tkinter

Python Tkinter menu bars don't display


Based on some comments you made to one of the answers, you are apparently running this on a Macintosh. The code works fine, but the menu appears in the mac menubar rather than on the window like it does on Windows and Linux. So, there's nothing wrong with your code as far as the menubar is concerned.


Code with Explanation

From personal experience, I have found that it is usually easier to manage all widgets in a widgets method. That is what I did here, and it worked. Also, instead of parent, I used master. I will now walk you through the code step-by-step.

from Tkinter import *

We import Tkinter (GUI stuff)

class App(Frame):

We create a class called App, which is the Frame where widgets are held.

    def __init__(self, master):        Frame.__init__(self, master)        self.grid()        self.widgets()

We create a method called __init__. This initializes the class, and runs another method called widgets.

    def widgets(self):        menubar = Menu(root)        menubar.add_command(label="File")        menubar.add_command(label="Quit", command=root.quit())        root.config(menu=menubar)

We create the widgets method. This is where the widget, menubar is added. If we were to create anymore widgets, they would also be here.

root=Tk()root.title("Menubar")app=App(root)root.mainloop()

Lastly, we give the entire window some properties. We give it a title, Menubar, and run the App class. lastly, we start the GUI's mainloop with root.mainloop.


I am trying the code as above, but all I get is "Python" on the macOS menubar and it's usual pulldown. tkinter just doesn't seem to work menus on macOS 10.14.1

I think what is happening is that there are mac specific fixups that are changing the event codes so certain menu items will end up under the Python menu item instead of where expected, I saw some of this in my own experiments. When I expanded my code and used some of the reserved FILE event codes instead of the standard ones, things worked better.

#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7# -*- coding: utf-8 -*-# -*- coding: utf-8 -*-from tkinter import *class App(Frame):    def __init__(self, master):        Frame.__init__(self, master)        self.grid()        self.widgets()    def widgets(self):        menubar = Menu(root)        menubar.add_command(label = 'File')        menubar.add_command(label = 'quit', command = root.quit())        root.config(menu = menubar)root = Tk()root.title('Menubar')app = App(root)root.mainloop()