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.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.