Python Tkinter Exit Program

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.

python tkinter exit program
Python Tkinter Exit Program

You may like the following Python tutorials:

So in this tutorial, we have learned how to exit the program in Python Tkinter or how to quit the program in python Tkinter.