Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用按钮销毁程序中当前打开的每个屏幕_Python_Python 3.x_Tkinter - Fatal编程技术网

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

Python 如何使用按钮销毁程序中当前打开的每个屏幕,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在创建一个编程项目,它将使用带有Tkinter GUI的matplotlib模拟细菌。用户登录后,有两个选项。一个是继续进行程序的主要部分,第二个选项是退出程序。我想知道,一旦按下“退出”按钮,如何关闭整个程序 我尝试使用当前打开的.destroy()命令销毁每个屏幕,但是我收到了多条错误消息,我不确定这是错误的原因 def shutdown(): screen.destroy() screen2.destroy() screen3.destroy() scr

我正在创建一个编程项目,它将使用带有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()


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

您的函数应该如下所示:

def shutdown():
screen.destroy()

如果我的答案对您有效,请将其标记为已接受的解决方案,以便其他用户知道此问题已得到解决(而不是仅仅看到发布了一个答案,不管它是否有效)。如果我的答案不适用于您,请发表评论,让我知道我的代码给您的错误。
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