- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 exit from Python using a Tkinter Button?
To exit from Python using a Tkinter button, you can follow the steps given below −
Import the tkinter library and create an instance of tkinter frame.
Set the size of the frame using geometry method.
Define a function close() to close the window. Call the method win.destroy() inside close().
Next, create a button and call the close() function.
Finally, run the mainloop of the application window.
# Import the library from tkinter import * # Create an instance of window win = Tk() # Set the geometry of the window win.geometry("700x350") # Title of the window win.title("Click the Button to Close the Window") # Define a function to close the window def close(): #win.destroy() win.quit() # Create a Button to call close() Button(win, text= "Close the Window", font=("Calibri",14,"bold"), command=close).pack(pady=20) win.mainloop()
On execution, it will produce the following output −
On clicking the button, it will close the window.
Instead of win.destroy(), you can also use win.quit() to close the application. However, there is a subtle difference between the two. win.quit() abruptly quits the application which means the mainloop will still be running in the background. win.destroy() on the other hand terminates the mainloop and destroys all the widgets inside the window.
- How to exit from a Python if clause?
- How to make a Button using the Tkinter Canvas widget?
- Add style to Python tkinter button
- How to create a Tkinter toggle button?
- How to change Tkinter Button state from disabled to normal?
- Add image on a Python Tkinter button
- Creating a button in tkinter in Python
- How to reset the background color of a Python Tkinter button?
- How to Disable / Enable a Button in TKinter?
- How to update a Button widget in Tkinter?
- How to highlight a tkinter button in macOS?
- How to create a Button on a Tkinter Canvas?
- How to bind a key to a button in Tkinter?
- Disable Exit (or [ X ]) in Tkinter Window
- How to handle a Button click event in tkinter?