栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使tkinter窗口出现在任务栏中

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394953.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号