Lawrence
Lawrence

Reputation: 25

Don't know how to apply mainloop() in my app

I have programmed a 2600 lines application which worked great when running from the IDE. Now I have created an executable via Pyinstaller and now the GUI does not start. The application starts and disappears quickly. I do not get any errors (anymore, solved them), however this problem remains. I think it has to do with missing the mainloop() in my application, which I don't know how to apply in this particular case. Usually its like this:

root = tk.Tk()
root.mainloop()

In my case I created a class for my window adding a menubar and label as statusbar (the latter not shown in my code below). This makes me assigning this class being a Tk() to main_window. Where do I put the mainloop() without getting an error?

I already tried: main_window.mainloop() since main_window is the window where all frames put on to, but then I get the following error in the IDE: main_window.mainloop() AttributeError: 'AppWindow' object has no attribute 'mainloop'

How do I apply the mainloop() into my application without getting the above mentioned error? Or how do I get my GUI to be working in a different manner? Both answers are welcome.

Here is the necessary code to know:

import tkinter as tk

class AppWindow():
    def __init__(self, master):
        self.master = master
        master.title("Basic Application")
        master.geometry("1060x680")
        master.grid_propagate(False)
        
        #create drop down menu
        self.menubar = tk.Menu(master) # main menubar
        
        #Add filemenu
        self.filemenu = tk.Menu(self.menubar, tearoff=0) #sub menu
        
        self.filemenu.add_separator() #create a bar in the menu
        self.filemenu.add_command(label="Quit", command=master.destroy) #Add submenu item
        self.menubar.add_cascade(label="File", menu=self.filemenu) #Add submenu to menubar
        
        self.master.config(menu=self.menubar) #Show menu


class FrameOne(tk.Frame):
    def __init__(self, parent):
        super().__init__()
        self["borderwidth"]=5
        self["relief"]="ridge"
                                      
        self.create_widgets() #Function which creates all widgets
        self.position_widgets() #Function which position all widgets
        
        
    def create_widgets(self): #Function which creates all widgets
        pass
    
    def position_widgets(self): #Function which position all widgets
        pass


#Create a window as defined in the AppWindow class
main_window = AppWindow(tk.Tk()) 

#Create a Frame as defined in class FrameOne
first_frame = FrameOne(main_window)
first_frame.grid(row=0, column=0) #Positioning Frame on Window

main_window.mainloop() #THIS PROVIDES AN ERROR | GUI DOES NOT START WITHOUT

Upvotes: 0

Views: 226

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385910

mainloop is a method on the tkinter root window and on tkinter itself. Like the error says, it's not a method of your AppWindow class.

In your case you should do it like this:

root = tk.Tk()
main_window = AppWindow(root)
root.mainloop()

Upvotes: 1

Related Questions