python - Tkinter,Windows : How to view window in windows task bar which has no title bar?

标签 python windows tkinter python-3.4

我创建了一个窗口:

root = Tk()

并删除了标题栏:

root.overrideredirect(True)

现在窗口不在windows的任务栏上了。我怎样才能在任务栏中显示它? (如果其他窗口在我的顶部,我只想将我的窗口放在前面)

最佳答案

Tk 不提供一种方法来让顶层窗口显示在任务栏上。overrideredirect 设置。为此,窗口需要有 WS_EX_APPWINDOW应用了扩展样式,并且这种类型的 Tk 窗口设置了 WS_EX_TOOLWINDOW。我们可以使用 python ctypes 扩展来重置它,但我们需要注意 Windows 上的 Tk 顶层窗口不直接由窗口管理器管理。因此,我们必须将这种新样式应用于 winfo_id 方法返回的窗口的父级。

下面的例子展示了这样一个窗口。

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.withdraw()
    root.after(10, root.deiconify)

def main():
    root = tk.Tk()
    root.wm_title("AppWindow Test")
    button = ttk.Button(root, text='Exit', command=root.destroy)
    button.place(x=10, y=10)
    root.overrideredirect(True)
    root.after(10, set_appwindow, root)
    root.mainloop()

if __name__ == '__main__':
    main()

关于python - Tkinter,Windows : How to view window in windows task bar which has no title bar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786337/

相关文章:

windows - 获取扬声器音频信号,然后将其流式传输

python - Canvas 更新变慢(可能需要释放一些对象)

python - 使 Pandas groupby 的行为类似于 itertools groupby

c# - 直接打印时 GhostScript 挂起

python - 如何进行prefetch_相关?我做错了吗?

python - 在 Python 中检测计算机/程序关闭?

框架内带有标签的 Python 类

python - 为什么 Tkinter Toplevel 对象被销毁?

python - 循环进口困惑

python - 检查python中的多个条件