Home > Mobile >  Why does my Tkinter GUI WIndow not open and not show any error with a Menubar
Why does my Tkinter GUI WIndow not open and not show any error with a Menubar

Time:06-14

I'm trying to add a Menubar to my Tkinter GUI. I was following an old python 3.4 tutorial and after checking with other sources it doesn't seem to be outdated. When i execute the code, i just get the Process finished with exit code 0 message, nothing else besides the python3 command running my code file.

Here is my code, i cut it down, but it should be possible to replicate the problem. I think there is some problem with the variable container, but I'm not sure how to solve the problem

import tkinter as tk


class Tutorial(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "Tkinter Tutorial")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        menubar = tk.Menu(container)
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Save graph") #, command=lambda: PageGraph.save_file()
        filemenu.add_command(label="Open graph") #, command=lambda: PageGraph.open_file()
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=quit())
        menubar.add_cascade(label="File", menu=filemenu)

        tk.Tk.config(self, menu=menubar)

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

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


app = Tutorial()
app.mainloop()

CodePudding user response:

Consider this line of code:

filemenu.add_command(label="Exit", command=quit())

This is functionally identical to this:

result_of_quit = quit()
filemenu.add_command(label="Exit", command=result_of_quit)

See the problem? You're immediately calling quit(), so tkinter quits.

When you define the command parameter of a widget you must give it a reference to a function. You can do that like in the following example. Pay attention to the missing ().

filemenu.add_command(label="Exit", command=quit)
  • Related