如何使Tkinter窗口跳到前面?

80

如何使Tkinter应用程序跳转到前台?目前,窗口显示在我的所有其他窗口后面,并且没有焦点。

是否有一些我应该调用的方法?

12个回答

1
在macOS High Sierra上,使用py3.6.4,这是我的解决方案:
def OnFocusIn(event):
    if type(event.widget).__name__ == 'Tk':
        event.widget.attributes('-topmost', False)

# Create and configure your root ...

root.attributes('-topmost', True)
root.focus_force()
root.bind('<FocusIn>', OnFocusIn)

这个想法是将其置于前台,直到用户与之交互,即获得焦点。
我尝试了被接受的答案 .after_idle().after()。但它们在某些情况下失败:当我直接从像PyCharm这样的IDE中运行我的脚本时,应用程序窗口将会停留在后面。
我的解决方案适用于我遇到的所有情况。

0

这将把窗口提到最前面,并且聚焦于该窗口。

def lift_window(window):
    window.attributes('-topmost',True)
    window.attributes('-topmost',False) # disable the topmost attribute after it is at the front to prevent permanent focus 
    window.focus_force() # focus to the window

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