Tk没有提供一种方法来使具有设置了 overrideredirect
的顶级窗口出现在任务栏上。为此,该窗口需要应用WS_EX_APPWINDOW扩展样式,并且此类型的Tk窗口已设置WS_EX_TOOLWINDOW。我们可以使用python
ctypes扩展名来重置此设置,但需要注意的是Windows上的Tk顶级窗口不是由窗口管理器直接管理的。因此,我们必须将此新样式应用于该
winfo_id方法返回的窗口的父级。
以下示例显示了这样一个窗口。
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.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()


