gpt4 book ai didi

python - 为什么 Tkinter 应用程序在调用退出后没有关闭

转载 作者:行者123 更新时间:2023-12-01 09:13:18 25 4
gpt4 key购买 nike

我正在使用 tkinter 开发一个应用程序。它由几个框架( window )叠在一起组成。我可以通过按钮从一个窗口转到另一个窗口。我根据我在 Stack Overflow 上看到的一个示例构建了它,但我不记得它的链接。

我怀疑由于我所做的选择,在调用 self.quit() 方法后 tkinter 无法正确关闭。下面的示例是我的应用程序的简化版本,尽管它与完整应用程序具有相同的结构。

通过下面的代码,您可以看到应用程序不会在整个程序结束之前关闭,即在我设置的 10 秒延迟之后。

import tkinter as tk
import tkinter.ttk as ttk
import time


class App:
"""
Inherited from the Frame class
"""

def __init__(self):
self.root = tk.Tk()
self.root.title('Test')

# This frame will contain all windows
container = ttk.Frame(self.root)
container.pack(side="top", fill="both", expand=True)

# Create different pages/windows of the application
self.frames = {}
self.frames["Window1"] = Window1(parent=container, controller=self)
self.frames["Window2"] = Window2(parent=container, controller=self)
self.frames["Window3"] = Window3(parent=container, controller=self)

self.frames["Window1"].grid(row=0, column=0, sticky="nsew")
self.frames["Window2"].grid(row=0, column=0, sticky="nsew")
self.frames["Window3"].grid(row=0, column=0, sticky="nsew")

self.show_frame("Window1")

def run(self):
self.root.deiconify
self.root.mainloop()

def show_frame(self, page_name):
'''
Show a frame for the given page name
'''
frame = self.frames[page_name]
frame.tkraise()


class Window1(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)

# Go to window 2
self.buttonW2 = ttk.Button(self, text='Go to 2', command=lambda: controller.show_frame("Window2"))
self.buttonW2.grid(row=0, column=0, sticky='NSE')

# Go to window 3
self.buttonW3 = ttk.Button(self, text='Go to 3', command=lambda: controller.show_frame("Window3"))
self.buttonW3.grid(row=0, column=1, sticky='NSE')

# EXIT button
self.buttonExit = ttk.Button(self, text='Exit', command=self.quit)
self.buttonExit.grid(row=0, column=2, sticky='NSE')


class Window2(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
# Go back button
buttonBack = ttk.Button(self, text="Back to 1", command=lambda: controller.show_frame("Window1"))
buttonBack.grid(row=0, column=3, sticky="NSE")


class Window3(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
# Go back button
buttonBack = ttk.Button(self, text="Back to 1", command=lambda: controller.show_frame("Window1"))
buttonBack.grid(row=1, column=1, sticky="E")


if __name__ == '__main__':
app = App()
app.run()

print('The App should be closed')

time.sleep(10)

print('The App is now closed')

有人可以帮我吗?

谢谢

最佳答案

Tkinters quit 命令只是停止 mainloop 但不会销毁窗口。如果您希望删除窗口,请使用destroy

因此创建退出按钮,如下所示:

    self.buttonExit = ttk.Button(self, text='Exit', command=self.controller.root.destroy)
self.buttonExit.grid(row=0, column=2, sticky='NSE')

关于python - 为什么 Tkinter 应用程序在调用退出后没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51467512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com