Tkinter,Windows:如何在没有标题栏的 Windows 任务栏中查看窗口? [英] Tkinter, Windows: How to view window in windows task bar which has no title bar?
问题描述
我创建了一个窗口:
root = Tk()
并移除标题栏:
root.overrideredirect(True)
现在窗口不在 Windows 的任务栏上.如何在任务栏中显示它?(如果其他窗户在我的顶部,我只想把我的窗户放在前面)
Now the window is not on the task bar in windows. How can i show it in the task bar? (I only want to bring my window to the front if other windows are on top of mine)
推荐答案
Tk 没有提供一种方法来让顶层窗口具有 overrideredirect 设置以显示在任务栏上.为此,窗口需要具有 WS_EX_APPWINDOW 应用了扩展样式,这种类型的 Tk 窗口设置了 WS_EX_TOOLWINDOW.我们可以使用 python ctypes 扩展来重置它,但我们需要注意 Windows 上的 Tk 顶级窗口不是由窗口管理器直接管理的.因此,我们必须将这种新样式应用于 winfo_id
方法返回的窗口的父级.
Tk does not provide a way to have a toplevel window that has overrideredirect set to appear on the taskbar. To do this the window needs to have the WS_EX_APPWINDOW extended style applied and this type of Tk window has WS_EX_TOOLWINDOW set instead. We can use the python ctypes extension to reset this but we need to note that Tk toplevel windows on Windows are not directly managed by the window manager. We have therefore to apply this new style to the parent of the windows returned by the winfo_id
method.
以下示例显示了这样一个窗口.
The following example shows such a window.
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()
这篇关于Tkinter,Windows:如何在没有标题栏的 Windows 任务栏中查看窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!