5

I'm trying to make a GUI using Tkinter and have come to implementing a menu bar. I've looked at a few tutorials and written some code for it, but a menu bar never seems to appear - just a blank frame with a white background. This doesn't just happen for my code though; on copying and pasting the code of one of the aforementioned tutorials into a new script, the same behaviour is exhibited.

I'd appreciate it if anyone could shed any light on what's causing this. My system is OS X 10.5, Python 2.7, Tk 8.4. Here's the code from the tutorial that doesn't appear to work:

#!/usr/local/bin/python2.7

from Tkinter import *
from ttk import *

class App(Frame):
    def __init__(self):
            Frame.__init__(self)

            self.master.geometry('400x300')
            self.master.title(__file__)

            self.pack()

            self.menu = Menu(tearoff=False)
            self.master.config(menu = self.menu)

            fm = self.file_menu = None
            fm = Menu(self.menu, tearoff=False)
            self.menu.add_cascade(label='File', menu = fm)

            fm.add_command(label='Say Hello', command = self.say_hello)
            fm.add_separator()
            fm.add_command(label='Quit', command = self.quit)

            self.mainloop()

    def say_hello(self, *e):
            self.label = Label(self.master, text='Hello there!')
            self.label.pack(anchor=CENTER, fill=NONE, expand=YES, side=LEFT)

if __name__ == '__main__':
    App()

and my code is here:

from Tkinter import *

class App(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        parent.title("Cluedo Solver 1.0")

        menubar = Menu(root)
        menubar.add_command(label="File")
        menubar.add_command(label="Quit", command=root.quit())

        root.config(menu=menubar)

root=Tk()
root.geometry("300x250+300+300")
app=App(root)
root.mainloop()
user2163043
  • 361
  • 1
  • 3
  • 14
  • 2
    Your example never creates self.master, is this your actual code? – Bryan Oakley Mar 12 '13 at 23:09
  • Thanks for the reply! This isn't actually my code, it's someone else's from one of the tutorials I was using, but they both suffer from the same problem so I figured it'd be better to paste this one. I'll edit my post to include mine. – user2163043 Mar 13 '13 at 00:05
  • Also you're right, I hadn't noticed that about self.master - guess it was written in a hurry! If I put a root=Tk(), put root as an argument of App's __init__ (and feed it into the last line), put a self.master=root immediately into __init__ and put root.mainloop() at the end of the script, I still find the same result... – user2163043 Mar 13 '13 at 00:12
  • @user2163043 Can you please post the tutorial? I am assuming that it is online and it would greatly aid us in helping you out. – xxmbabanexx Mar 13 '13 at 00:48
  • @user2163043 Solved. Fun problem - where can I find out more about `Menu`'s? Also, if my answer was helpful please click the green checkmark next to it. This will help others recognize this as potentially helpful. – xxmbabanexx Mar 13 '13 at 00:59
  • Are you by any chance running this code on a Macintosh? – Bryan Oakley Mar 13 '13 at 13:41

5 Answers5

22

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
4

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.

xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
  • @user2163043 If my answer was at all helpful to you, please give me the green check mark. It will help others see that this is a good answer. If not, please tell me the issue with my answer. – xxmbabanexx Mar 13 '13 at 01:44
  • Wow, hi thanks for your answer! Strangely though when I run it, I don't actually get a pop-up gui at all - python launcher runs, but brings up no window. If I place in a root.geometry("250x300+300+300") after root.title however, it does the same thing as before - bring up an empty white frame (this line was in my original code too - sorry forgot to mention it, have edited my question now). Very strange. The tutorial is here: http://skriticos.blogspot.co.uk/2009/07/tkinterttk-tutorial-part-2-menu-label.html. Thanks for your help! – user2163043 Mar 13 '13 at 12:34
  • @user2163043 I have a mac also - I will run the program there as well. So... Does the menubar show up or not? It worked well for me... What could have happened is that the Mac Menubar shows up in the upper bar, the area next to the apple sign and your program name. That, is actually almost certainly what happened. – xxmbabanexx Mar 13 '13 at 12:52
  • Ahhhhh ok I feel quite stupid now, yes that is what happened! The menu was there all along but after 4 years of owning a Mac I still didn't think to look for it in the OS X menu bar. Great. Thanks so much for your help I definitely wouldn't have found it otherwise! – user2163043 Mar 13 '13 at 13:12
  • @user2163043 Yea - SO is great. I have learned so much since I joined. Try answering some questions - oftentimes you learn more from answering than asking. Anyways, good luck on your project. – xxmbabanexx Mar 13 '13 at 13:44
  • I'm trying this code with python3 on OSX 10.11.6 in 2017 and I don't see a menu any where. when I run a tiny little box shows up in the middle of the screen. I can stretch that out to make a box, but I don't see the menu bar any where. At the top left I just see python, no menu bars. – kdubs Jul 24 '17 at 02:50
  • after a little digging I found this : https://stackoverflow.com/questions/27910640/how-to-make-menu-add-command-work-in-tkinter-on-the-mac you can only use cascade menus at the top level on a mac – kdubs Jul 25 '17 at 02:03
1

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()
Douglas Goodall
  • 121
  • 1
  • 5
0

Check your mac menu bar if you are doing any GUI that involves menubar which you will like to view or test. Its subtle and you may think you code is not to working. Click on the app(in this case python window), it will show a drop down menubar.

0

guys to solve the problem

enter image description here

look the file part that's where the menubar for mac is located

cottontail
  • 10,268
  • 18
  • 50
  • 51