
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 to close a Tkinter window by pressing a Button?
Tkinter initially creates a window or frame that contains widgets and Labels within it. Let us suppose we want to close the tkinter window with a button. A Button is a UI widget that can be used to perform a certain operation.
Example
Here, we will create a button that closes the tkinter window. In order to close and terminate the TCL interpreter, we mainly use the destroy() method.
#Import the required libraries from tkinter import * #Create an instance of tkinter frame win= Tk() #Set the geometry of frame win.geometry("600x250") #Create a Label Label(win, text="Press the Close Button to close the window", font=('Helvetica bold', 11)).pack(pady=20) #Define a function to close the window def close_win(): win.destroy() #Create a Button for closing the window button= Button(win, text="Close", command=close_win) button.pack(pady=20) win.mainloop()
Output
Running the above code will display a window containing a button that can be triggered to close the window or frame.
Now click the “Close” button to close the window.
- Related Questions & Answers
- How to open a new window by the user pressing a button in a tkinter GUI?
- How do I close a tkinter window?
- Function to close the window in Tkinter
- Automatically close window after a certain time in Tkinter
- How to bind the Escape key to close a window in Tkinter?
- How to close only the TopLevel window in Python Tkinter?
- Call the same function when clicking a Button and pressing Enter in Tkinter
- How do I handle the window close event in Tkinter?
- How to add a margin to a tkinter window?
- How to create a Tkinter toggle button?
- How to disable close button on a JFrame in Java?
- How to close the whole browser window by keeping the webDriver active?
- How to make a Tkinter window not resizable?
- How to delete Tkinter widgets from a window?
- How to set a Tkinter window to a constant size?
Advertisements