Tkinter, Windows: How to view window in windows task bar which has no title bar? Tkinter, Windows: How to view window in windows task bar which has no title bar? tkinter tkinter

Tkinter, Windows: How to view window in windows task bar which has no title bar?


Tk does not provide a way to have a toplevel window that has overrideredirect set to appear on the taskbar. To do this the window needs to have the WS_EX_APPWINDOW extended style applied and this type of Tk window has WS_EX_TOOLWINDOW set instead. We can use the python ctypes extension to reset this but we need to note that Tk toplevel windows on Windows are not directly managed by the window manager. We have therefore to apply this new style to the parent of the windows returned by the winfo_id method.

The following example shows such a window.

import tkinter as tkimport tkinter.ttk as ttkfrom ctypes import windllGWL_EXSTYLE = -20WS_EX_APPWINDOW = 0x00040000WS_EX_TOOLWINDOW = 0x00000080def set_appwindow(root):    hwnd = windll.user32.GetParent(root.winfo_id())    style = windll.user32.GetWindowLongPtrW(hwnd, GWL_EXSTYLE)    style = style & ~WS_EX_TOOLWINDOW    style = style | WS_EX_APPWINDOW    res = windll.user32.SetWindowLongPtrW(hwnd, GWL_EXSTYLE, style)    # re-assert the new window style    root.withdraw()    root.after(10, root.deiconify)def main():    root = tk.Tk()    root.wm_title("AppWindow Test")    button = ttk.Button(root, text='Exit', command=root.destroy)    button.place(x=10, y=10)    root.overrideredirect(True)    root.after(10, set_appwindow, root)    root.mainloop()if __name__ == '__main__':    main()


A simplification of @patthoyts's answer:

# Partially taken from: https://stackoverflow.com/a/2400467/11106801from ctypes.wintypes import BOOL, HWND, LONGimport tkinter as tkimport ctypes# Defining functionsGetWindowLongPtrW = ctypes.windll.user32.GetWindowLongPtrWSetWindowLongPtrW = ctypes.windll.user32.SetWindowLongPtrWdef get_handle(root) -> int:    root.update_idletasks()    # This gets the window's parent same as `ctypes.windll.user32.GetParent`    return GetWindowLongPtrW(root.winfo_id(), GWLP_HWNDPARENT)# ConstantsGWL_STYLE = -16GWLP_HWNDPARENT = -8WS_CAPTION = 0x00C00000WS_THICKFRAME = 0x00040000if __name__ == "__main__":    root = tk.Tk()    hwnd:int = get_handle(root)    style:int = GetWindowLongPtrW(hwnd, GWL_STYLE)    style &= ~(WS_CAPTION | WS_THICKFRAME)    SetWindowLongPtrW(hwnd, GWL_STYLE, style)

The style &= ~(WS_CAPTION | WS_THICKFRAME) just removes the title bar of the window. For more info read Microsoft's documentation.


Not really needed but to make it safer use this to define the functions:

# Defining typesINT = ctypes.c_intLONG_PTR = ctypes.c_longdef _errcheck_not_zero(value, func, args):    if value == 0:        raise ctypes.WinError()    return args# Defining functionsGetWindowLongPtrW = ctypes.windll.user32.GetWindowLongPtrWGetWindowLongPtrW.argtypes = (HWND, INT)GetWindowLongPtrW.restype = LONG_PTRGetWindowLongPtrW.errcheck = _errcheck_not_zeroSetWindowLongPtrW = ctypes.windll.user32.SetWindowLongPtrWSetWindowLongPtrW.argtypes = (HWND, INT, LONG_PTR)SetWindowLongPtrW.restype = LONG_PTRSetWindowLongPtrW.errcheck = _errcheck_not_zero

I know the question specifically says it's about Windows but for Linux, this should work.