Make tkinter Window appear in the taskbar

前端 未结 1 522
予麋鹿
予麋鹿 2020-12-19 09:43

I\'m making a music player GUI and I can\'t make it appear on the taskbar or in Alt-Tab. I have set overrideredirect() to true to remove the borders and the title. I also ma

相关标签:
1条回答
  • 2020-12-19 10:40

    after a bit of research i have found that there is a way to do this, but it involves using ctypes and is a windows only solution:

    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.GetWindowLongW(hwnd, GWL_EXSTYLE)
        style = style & ~WS_EX_TOOLWINDOW
        style = style | WS_EX_APPWINDOW
        res = windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, style)
        # re-assert the new window style
        root.wm_withdraw()
        root.after(10, lambda: root.wm_deiconify())
    
    def main():
        root = tk.Tk()
        root.wm_title("AppWindow Test")
        button = ttk.Button(root, text='Exit', command=lambda: root.destroy())
        button.place(x=10,y=10)
        root.overrideredirect(True)
        root.after(10, lambda: set_appwindow(root))
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    

    this involves using ctypes to manipulate the windows style, however you need to use the right Get/Set functions depending on the applications environment. for 32 bit windows it seems you need to use either:
    GetWindowLongW and SetWindowLongW
    or
    GetWindowLongA and SetWindowLongA

    but 64 bit needs:
    GetWindowLongPtrW and SetWindowLongPtrW
    or
    GetWindowLongPtrA and SetWindowLongPtrA
    see this

    or alternatively, if you want this behaviour by default:

    import tkinter as tk
    
    from ctypes import windll
    
    class Tk(tk.Tk):
        def overrideredirect(self, boolean=None):
            tk.Tk.overrideredirect(self, boolean)
            GWL_EXSTYLE=-20
            WS_EX_APPWINDOW=0x00040000
            WS_EX_TOOLWINDOW=0x00000080
            if boolean:
                print("Setting")
                hwnd = windll.user32.GetParent(self.winfo_id())
                style = windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
                style = style & ~WS_EX_TOOLWINDOW
                style = style | WS_EX_APPWINDOW
                res = windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, style)
            self.wm_withdraw()
            self.wm_deiconify()
    
    0 讨论(0)
提交回复
热议问题