How to Close a Tkinter Window With a Button

We can use a function or command attached to a button in the Tkinter GUI to close the Tkinter window when the user clicks it.

root.destory() Class Method

try:
    import Tkinter as tk
except:
    import tkinter as tk
    

class Test():
   def __init__(self):
       self.root = tk.Tk()
       self.root.geometry('100x50')
       button = tk.Button(self.root, text = 'Click and Quit', command=self.quit)
       button.pack()
       self.root.mainloop()

   def quit(self):
       self.root.destroy()
        
app = Test()

destroy() destroys or closes the window.

Tkinter close a window with a button

destroy() Non-Class Method

try:
    import Tkinter as tk
except:
    import tkinter as tk

root = tk.Tk()
root.geometry("100x50")

def close_window ():
    root.destroy()

button = tk.Button(text = "Click and Quit", command = close_window)
button.pack()

root.mainloop()