hi everyone.....
Can someone tell me 'how can we disable the close icon' which is found on upper left corner of a tkinter menu which is encircled in the picture below:
Thanks in advance

Dani AI

Generated

Vegaseat’s approach is the portable way to “disable” the X: handle the window manager’s close request yourself and decide what to do (e.g., ignore it, prompt, or minimize). That protocol is cross‑platform in Tk; by default Tk destroys the window on WM_DELETE_WINDOW, but you can replace that handler with your own logic. See the Tk wm protocol docs for details. (tcl.tk)

Gleam.uahmed’s overrideredirect(1) removes all window manager decorations (not just the close button) and tells the WM to ignore the window. That is useful for splash/kiosk windows, but it also prevents normal moving/resizing, can interfere with focus/stacking, and varies by platform/WM. Use it only if you truly want a frameless window. (tcl.tk, manpages.org)

If you specifically need to disable the close button on Windows (rather than just ignore the close event), you can edit the system menu via Win32. This greys out/removes the Close item and updates the title bar. Still keep a WM_DELETE handler as a fallback.

# Windows-only: remove the Close menu item and update the title bar
import tkinter as tk
import ctypes

root = tk.Tk()

hwnd = root.winfo_id()
user32 = ctypes.windll.user32
hMenu = user32.GetSystemMenu(hwnd, False)
user32.DeleteMenu(hMenu, 0xF060, 0)   # SC_CLOSE
user32.DrawMenuBar(hwnd)

root.mainloop()

The calls above use GetSystemMenu to obtain the window’s system menu, delete SC_CLOSE, then redraw the menu bar. Note that Windows routes the close button and Alt+F4 through WM_SYSCOMMAND/SC_CLOSE, so modifying the system menu affects those paths too. (learn.microsoft.com)

Practical tips:

  • Always provide your own explicit Exit/Quit UI so users are not trapped.
  • Disabling close is not security; users can still kill the process externally.

Recommended Answers

All 3 Replies

Here is an example how to do this with the Windows OS ...

# disable window title bar corner x click
# you need to supply your own exit button

from Tkinter import *

def exit():
    "dummy function"
    pass

root = Tk()
root.protocol("WM_DELETE_WINDOW", exit)

btn_quit = Button( text="Quit", command=root.destroy )
btn_quit.pack()

root.mainloop()

thanks you vegaseat. you are the best......

and for Linux user

replace

root.protocol("WM_DELETE_WINDOW", exit)

with this

root.overrideredirect(1)
commented: Usefull info but to old thread. +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.