In this section, we will learn how to exit the program in Python Tkinter.
Python Tkinter Exit Program
To quit a program we need to destroy the window. There is a built-in method destroy
()
to close the window in Python Tkinter.
Syntax:
Here ws is the main window & is the variable for Tk() class method.
ws.destroy()
Here is the the small demonstration of Python Tkinter Exit Program.
Code:
In this code, we have created a simple button on the window. When the user will click on the button it will close the window.
from tkinter import *
from tkinter import messagebox
from time import *
def close():
ws.destroy()
ws = Tk()
ws.title("PythonGuides")
ws.geometry("400x300")
ws['bg']='#5d8a82'
Button(
ws,
text="EXIT PROGRAM",
font=("Times", 14),
command=close
).pack(pady=100)
ws.mainloop()
Output:
In this output, Button with the text “Exit Program” is displayed. When user will click on this button the window will close.
You may like the following Python tutorials:
- Python Tkinter TreeView Example
- Python Tkinter ToDo List
- Python Tkinter Window Size
- Python Tkinter Canvas Tutorial
- Python Tkinter Progress bar
- Python Tkinter Stopwatch
- Python Tkinter Listbox
- How to go to next page in Python Tkinter Program
- How to Create Countdown Timer using Python Tkinter
So in this tutorial, we have learned how to exit the program in Python Tkinter or how to quit the program in python Tkinter.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.