Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter menu bars don't display

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()
like image 534
user2163043 Avatar asked Nov 28 '22 21:11

user2163043


1 Answers

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.

like image 188
Bryan Oakley Avatar answered Dec 06 '22 15:12

Bryan Oakley