使tkinter窗口出现在任务栏中

使tkinter窗口出现在任务栏中,第1张

使tkinter窗口出现在任务栏中

经过一些研究,我发现有一种方法可以执行此 *** 作,但是它涉及使用ctypes,并且是Windows的唯一解决方案:

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.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()

这涉及使用ctypes来 *** 纵Windows样式,但是您需要根据应用程序环境使用正确的Get / Set函数。对于32位窗口,看来您需要使用:

GetWindowLongW
SetWindowLongW


GetWindowLongA
SetWindowLongA


但64位的需要:

GetWindowLongPtrW
SetWindowLongPtrW


GetWindowLongPtrA
SetWindowLongPtrA

看到这

或者,如果您默认需要这种行为:

import tkinter as tkfrom ctypes import windllclass 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()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4989945.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存