让一个tkinter窗口显示在所有其他窗口之上

3
#!/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上,通知不会自动弹出到所有其他窗口的上方。第一次需要单击“kill”按钮(文本)并将其聚焦,之后根窗口将显示在所有其他窗口的上方。

有没有办法使通知在Windows上自动出现在所有其他窗口的上方?

在Linux上似乎运行得很好(Ubuntu 9.10)。

1个回答

9

根据这条消息,您应该能够在root.overridedirect(1)之后添加以下内容。这里进行了快速测试,表明它应该适用于您。

root.wm_attributes("-topmost", 1)

不太确定......我很少使用OSX。但是,我在这里使用Python 2.6.5在OSX 10.5.8上进行了测试,结果出现错误“_tkinter.TclError:bad option“-topmost”:必须是 -modified 或 -titlepath”。不知道那是什么意思。 - Peter Hansen
是的,我认为这个技巧只适用于Windows系统?它能在Linux上运行吗? - Skylar Saveland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接