不知道如何在我的应用程序中应用 mainloop()

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

我编写了一个 2600 行的应用程序,当 运行 来自 IDE 时,它运行良好。现在我已经通过 Pyinstaller 创建了一个可执行文件,但现在 GUI 没有启动。该应用程序启动并迅速消失。我没有收到任何错误(已经解决了),但是这个问题仍然存在。我认为这与我的申请中缺少 mainloop() 有关,我不知道如何在这种特殊情况下申请。通常是这样的:

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

在我的例子中,我为我的 window 创建了一个 class,添加了一个菜单栏和标签作为状态栏(后者没有显示在我下面的代码中) .这让我将此 class 分配给 main_window 作为 Tk()。我应该把 mainloop() 放在哪里而不会出错?

我已经试过了: main_window.mainloop() 因为 main_window 是所有帧所在的 window,但是我在 IDE 中收到以下错误: main_window.mainloop() AttributeError: 'AppWindow' object has no attribute 'mainloop'

如何将 mainloop() 应用到我的应用程序中而不出现上述错误?或者如何让我的 GUI 以不同的方式工作?欢迎两个答案。

这里是需要知道的代码:

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

mainloop 是 tkinter 根 window 和 tkinter 本身的一种方法。正如错误所说,这不是您的 AppWindow class.

的方法

在你的情况下你应该这样做:

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