Python tkinter中的窗口将自动关闭

Python tkinter中的窗口将自动关闭,python,tkinter,Python,Tkinter,我这里有一些从tkinter教程中复制的代码,因此我确信它是100%正确的: import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.pack() self.create_widgets() def c

我这里有一些从tkinter教程中复制的代码,因此我确信它是100%正确的:

import tkinter as tk


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")


root = tk.Tk()
app = Application(master=root)
app.mainloop()
它应该可以工作,但当我运行程序时,它会显示窗口约1毫秒,然后立即关闭。我该如何解决这个问题

我使用的是Python 3.8。

mainloop()
应该在
tk.tk
对象上调用,但在您的代码
app
中是
tk.Frame
对象。所以,试试

root.mainloop()

当我运行它时,它不会这样做,而且
按钮也会起作用。错误是不可再现的,投票结束这个问题。@Yatin OP在一个被删除的回答/评论中提到了python版本。这没关系-mainloop的行为与你调用它的小部件相同。
mainloop()
是一种通用方法,可以在任何tkinter小部件上调用。