Tkinter, Windows: How to view window in windows task bar which has no title bar?
我创建了一个窗口:
1 | root = Tk() |
并删除标题栏:
1 | root.overrideredirect(True) |
现在该窗口不在Windows的任务栏上。如何在任务栏中显示它? (如果其他窗户在我的顶部,我只想将窗户放到最前面)
Tk没有提供一种方法来使具有设置了overrideredirect的顶级窗口出现在任务栏上。为此,该窗口需要应用WS_EX_APPWINDOW扩展样式,并且此类型的Tk窗口已设置WS_EX_TOOLWINDOW。我们可以使用python ctypes扩展名来重置此设置,但需要注意的是Windows上的Tk顶级窗口不是由窗口管理器直接管理的。因此,我们必须将此新样式应用于
下面的示例显示了这样的窗口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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.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() |