- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I handle the window close event in Tkinter?
Tkinter provides a custom handler to close the window. It acts as a callback function that the user can run in order to close the window.
To close the window using the handler, we can use the destroy() method. It closes the window abruptly after calling it in any function or any widget. Let us invoke the close event handler by defining a method.
By using as an argument in Widget
Example
#Importing the required library from tkinter import * #Create an instance of tkinter frame or window win= Tk() #Set the geometry win.geometry("600x400") #Create a button and pass arguments in command as a function name my_button= Button(win, text= "X", font=('Helvetica bold', 20), borderwidth=2, command= win.destroy) my_button.pack(pady=20) win.mainloop()
By Invoking in a function
#Importing the required library from tkinter import * #Create an instance of tkinter frame or window win= Tk() #Set the geometry win.geometry("600x300") #Define a function def close(): win.destroy() #Create a button and pass arguments in command as a function name my_button= Button(win, text= "X", font=('Helvetica bold', 20), borderwidth=2, command= close) my_button.pack(pady=20) win.mainloop()
Output
Running the above code will create a button “X” and by clicking over that, we can close the main window.
- Related Articles
- How do I close a tkinter window?
- Function to close the window in Tkinter
- How to close only the TopLevel window in Python Tkinter?
- How do I create a popup window in Tkinter?
- How do I position the buttons on a Tkinter window?
- How do I set a minimum window size in Tkinter?
- How do I use Window Manager (wm) attributes in Tkinter?
- How do I open a website in a Tkinter window?
- How do I create a popup window using Tkinter?
- How do I get rid of the Python Tkinter root window?
- How to handle a Button click event in tkinter?
- How to bind the Escape key to close a window in Tkinter?
- How do I create a popup window using Tkinter Program?
- How to close a Tkinter window by pressing a Button?
- Automatically close window after a certain time in Tkinter

Advertisements