如何使用按钮销毁程序中当前打开的每个屏幕

2024-09-29 17:10:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个编程项目,它将使用带有Tkinter GUI的matplotlib模拟细菌。用户登录后,有两个选项。一个是继续进行程序的主要部分,第二个选项是退出程序。我想知道,一旦按下“退出”按钮,如何关闭整个程序

我尝试使用当前打开的.destroy()命令销毁每个屏幕,但是我收到了多条错误消息,我不确定这是错误的原因

def shutdown():
    screen.destroy()
    screen2.destroy()
    screen3.destroy()
    screen6.destroy()
    screen7.destroy()
    screen8.destroy()

def session():
    global screen8
    screen8 = Toplevel(screen)
    screen8.title("Dashboard")
    screen8.geometry("400x400")
    Label(screen8, text = "Welcome to the Dashboard").pack()
    Button(screen8, text = "Simulate Bacteria", command = simulate_bacteria).pack()
    Button(screen8, text = "Quit", command = shutdown).pack()

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\aliso\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\aliso\Desktop\CompSci Project\BacSim.py", line 15, in shutdown
    screen2.destroy()
  File "C:\Users\aliso\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2305, in destroy
    self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed

Tags: textinpy程序tkinterlinecallusers
1条回答
网友
1楼 · 发布于 2024-09-29 17:10:27

我相信这是因为您正在使用Toplevel,所以一旦根窗口(screen)被破坏,所有其他窗口也会被破坏

您的函数应该如下所示:

def shutdown():
    screen.destroy()

相关问题 更多 >

    热门问题