What does overrideredirect() do?

Question:

I have come to understand overrideredirect helps removing the default window decorations like the toolbar.What are its other uses? I am not very sure and couldn’t find much documentation.

I am working on a mac. Using tkinter I want to obtain window which remains maxsize and cannot be resized,which i have achieved using geometry and resizable. What I need now is the guarantee that no random keystrokes by my user can close the window. Will overrideredirect help me in that? Is there any alternative?

Asked By: momo

||

Answers:

You can use overrideredirect() and set its flag to True. This will disable your window to be closed by regular means as mentioed in the link above. By regular means, it is meant the X button and the Alt + F4 keystrokes combination.

Since you used geometry() and resizable(), you will need to call update_idletasks() to force the display to be updated before the application next idles.

Here is an example:

import Tkinter as Tk

root = Tk.Tk()
root.geometry('200x200+100+100')
root.resizable(False, False)
root.update_idletasks()
root.overrideredirect(True)
root.mainloop()

Drawback of this method: it always works on Microsoft Windows platform but it may not work on some Unix and MacOS platforms.

Edit:

You asked clarification about update_idletasks(), I think it is better if I quote directly from its documentation as it is clearer (but if you do not understand this quotation, please let me know):

Some tasks in updating the display, such as resizing and redrawing
widgets, are called idle tasks because they are usually deferred until
the application has finished handling events and has gone back to the
main loop to wait for new events.

If you want to force the display to be updated before the application next idles, call the w.update_idletasks() method on any
widget.

Answered By: Billal Begueradj

I have tried the sample code and I have the same error as in my code: root:overrideredirect(1)
NameError: name ‘overrideredirect’ is not defined

This is my code in case it helps:

import random
from tkinter import *
from random import randint
import time
import threading

root = Tk()
root.attributes("-alpha",0)
root:overrideredirect(1)
root.attributes("-topmost", 1)

def placewindows():
    while True:
        win = Toplevel(root)
        win.geometry("300x60"+str(randint(0, root.winfo_screenwidth() - 300)) + "+" + str(randint(0, root.winfo_screenheight() - 60)))
        win.overrideredirect(1)
        Label(win, text="I HACK YOU", fg="red").place(relx=.38, rely=.3)
        win.lift()
        win.attributes("-topmost", True)
        win.attributes("-topmost", False)
        root.lift()
        win.attributes("-topmost", True)
        win.attributes("-topmost", False)
        time.sleep(.05)
        
threading.Thread(target=placewindows).start()
root.mainloop()
Answered By: EL SAYA

You’re using a colon (:) instead of a period (.)

root:overrideredirect(1)

should be

root.overrideredirect(1)

Answered By: psimonson
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.