HusaibHasan
HusaibHasan

Reputation: 13

How to destroy every screen currently open in the program using a button

I'm creating a programming project which will simulate bacteria using matplotlib with a Tkinter GUI. Once the user has logged in there are 2 options. One is to continue to the main part of the program and the second option is to quit the program. I am wondering how to, once the "Quit" button has been pressed, to close the whole program.

I have tried destroying each screen using the .destroy() command which is currently open however I get multiple error messages and I'm not sure as to why this is wrong.

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

Upvotes: 1

Views: 88

Answers (1)

CME1 Crewmember
CME1 Crewmember

Reputation: 546

I believe it is because you are using Toplevel, so once the root window (screen) is destroyed, all the other windows also get destroyed.

Your function should look like this:

def shutdown():
    screen.destroy()

Upvotes: 1

Related Questions