Disable Exit (or [ X ]) in Tkinter Window

The window manager implements the Tkinter window control icons. To hide and show the Tkinter window control icons, we can use the built-in protocol() function, which describes whether we want to disable control icons' functionality.

To disable the Exit or [X] control icon, we have to define the protocol() method. We can limit the control icon definition by specifying an empty function for disabling the state of the control icon.

Syntax

window.protocol("WM_DELETE_WINDOW", callback_function)

Where:

  • WM_DELETE_WINDOW - Protocol for window close event
  • callback_function - Function to handle the close event (use empty function to disable)

Example

Here's how to disable the [X] button while providing an alternative close method ?

# Import the tkinter library
from tkinter import *
from tkinter import ttk

# Create an instance of Tkinter frame
win = Tk()

# Define the geometry of the window
win.geometry("750x250")
win.title("Disabled Close Button Example")

def close_win():
    win.destroy()

def disable_event():
    pass

# Create a button to close the window
btn = ttk.Button(win, text="Click here to Close", command=close_win)
btn.pack(pady=20)

# Add a label for instruction
label = ttk.Label(win, text="The [X] button is disabled. Use the button below to close.")
label.pack(pady=10)

# Disable the Close Window Control Icon
win.protocol("WM_DELETE_WINDOW", disable_event)

win.mainloop()

Output

The above code will display a window that has a disabled [X] window close control. When you click the [X] button, nothing happens. To close the window, you must click the "Click here to Close" button.

Disabled Close Button Example ? The [X] button is disabled. Use the button below to close. Click here to Close

Alternative: Confirm Before Closing

Instead of completely disabling the close button, you can show a confirmation dialog ?

from tkinter import *
from tkinter import ttk, messagebox

win = Tk()
win.geometry("400x200")
win.title("Confirm Close Example")

def confirm_close():
    if messagebox.askyesno("Confirm", "Are you sure you want to close?"):
        win.destroy()

# Create content
label = ttk.Label(win, text="Try closing this window with [X] button")
label.pack(pady=50)

# Set protocol to show confirmation dialog
win.protocol("WM_DELETE_WINDOW", confirm_close)

win.mainloop()

Key Points

  • The protocol() method intercepts window manager events
  • WM_DELETE_WINDOW is triggered when user clicks [X] button
  • Pass an empty function to completely disable closing
  • Use confirmation dialogs for better user experience

Conclusion

Use win.protocol("WM_DELETE_WINDOW", disable_event) to disable the [X] button in Tkinter. Always provide alternative closing methods when disabling the default close behavior.

Updated on: 2026-03-25T19:26:36+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements