How to close a tkinter window?

by hal.littel , in category: Third Party Scripts , 4 days ago

How to close a tkinter window?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 3 days ago

@hal.littel 

To close a tkinter window, you can use the destroy() method. Here is an example code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import tkinter as tk

# Create the tkinter window
root = tk.Tk()
root.title("My Window")

# Function to close the window
def close_window():
    root.destroy()

# Create a button to close the window
close_button = tk.Button(root, text="Close Window", command=close_window)
close_button.pack()

# Run the tkinter main loop
root.mainloop()


In this code, we create a button that calls the close_window() function when clicked. The close_window() function then calls the destroy() method on the root window to close it.