让tkinter窗口在所有窗口上方显示

3 投票
1 回答
5012 浏览
提问于 2025-04-15 17:07
#!/usr/bin/env python
# Display window with toDisplayText and timeOut of the window.

from Tkinter import *

def showNotification(notificationTimeout, textToDisplay):

    ## Create main window
    root = Tk()
    Button(root, text=textToDisplay, activebackground="white", bg="white", command=lambda: root.destroy()).pack(side=LEFT)

    root.update_idletasks()
    # Remove window decorations
    root.overrideredirect(1)

    timeOut = int(notificationTimeout*1000) # Convert to ms from s

    ## Run appliction
    root.after(timeOut,root.destroy)
    root.mainloop()

上面的代码创建了一个通知,并设置了超时时间。不过在Windows系统上,这个通知并不会自动显示在其他窗口的最上面。第一次需要点击一下“关闭”按钮(那个文本),然后把它聚焦,之后根窗口才会显示在所有其他窗口的上面。

有没有办法让这个通知在Windows上自动显示在所有其他窗口的上面呢?

在Linux系统上(比如Ubuntu 9.10)似乎是可以正常工作的。

1 个回答

9

根据这条消息,你应该可以在root.overridedirect(1)之后添加以下内容。这里做了个简单测试,结果显示这应该对你有效。

root.wm_attributes("-topmost", 1)

撰写回答