← Go Back
If you just want to cancel the window close event without asking for confirmation, better use:
How to Cancel Window Close Event in Tk (tkinter)
import tkinter as tk
from tkinter import messagebox
def delete_window():
# Function called when the user tries to close the window.
close = messagebox.askyesno(
message="Are you sure you want to close the application?",
title="Confirm Exit"
)
if close:
root.destroy()
root = tk.Tk()
# Replace Tk's default procedure with our own function to close the window.
root.protocol("WM_DELETE_WINDOW", delete_window)
root.mainloop()
If you just want to cancel the window close event without asking for confirmation, better use:
# Always disable window closing.
root.protocol("WM_DELETE_WINDOW", lambda: None)
🐍 You might also find interesting: