pythonwindowstkinterpython-3.4

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


I created a window:

root = Tk()

and removed the titlebar:

root.overrideredirect(True)

Now the window is not on the task bar in windows. How can i show it in the task bar? (I only want to bring my window to the front if other windows are on top of mine)


Solution

  • 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 tk
    import tkinter.ttk as ttk
    from ctypes import windll
    
    GWL_EXSTYLE = -20
    WS_EX_APPWINDOW = 0x00040000
    WS_EX_TOOLWINDOW = 0x00000080
    
    def 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()