繁体   English   中英

如何让 Tkinter 窗口跳到前面?

[英]How to make a Tkinter window jump to the front?

如何让 Tkinter 应用程序跳转到前面? 目前,该窗口出现在我所有其他窗口的后面并且没有获得焦点。

有什么方法我应该调用吗?

假设您说“我的其他窗口”时指的是您的应用程序窗口,则可以在 Toplevel 或 Tk 上使用lift()方法:

root.lift()

如果您希望窗口保持在所有其他窗口之上,请使用:

root.attributes("-topmost", True)

其中root是您的 Toplevel 或 Tk。 不要忘记-"topmost" topmost "topmost"

要使其成为临时的,请在以下之后立即禁用最上面:

def raise_above_all(window):
    window.attributes('-topmost', 1)
    window.attributes('-topmost', 0)

只需传入您想要作为参数提出的窗口,这应该有效。

在 mainloop() 之前添加以下几行:

root.lift()
root.attributes('-topmost',True)
root.after_idle(root.attributes,'-topmost',False)

它非常适合我。 它使窗口在生成窗口时出现在最前面,并且不会一直保持在前面。

如果您在 Mac 上执行此操作,请使用 AppleEvents 将焦点放在 Python 上。 例如:

import os

os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

关于 Mac,我注意到可能存在一个问题,如果有多个 Python GUI 正在运行,每个进程都会被命名为“Python”,而 AppleScript 会倾向于将错误的一个提升到前面。 这是我的解决方案。 这个想法是在加载 Tkinter 之前和之后获取正在运行的进程 ID 列表。 (请注意,这些是 AppleScript 进程 ID,它们似乎与它们的 posix 对应项无关。去算一下。)然后奇怪的人将是你的,你将那个人移到最前面。 (我不认为最后的循环是必要的,但是如果您只是获取 ID 为 procID 的每个进程,AppleScript 显然会返回一个由名称标识的对象,这当然是非唯一的“Python”,所以除非我遗漏了某些东西,否则我们将回到第一个。)

import Tkinter, subprocess
def applescript(script):
    return subprocess.check_output(['/usr/bin/osascript', '-e', script])
def procidset():
    return set(applescript(
        'tell app "System Events" to return id of every process whose name is "Python"'
        ).replace(',','').split())
idset = procidset()
root = Tkinter.Tk()
procid = iter(procidset() - idset).next()
applescript('''
    tell app "System Events"
        repeat with proc in every process whose name is "Python"
            if id of proc is ''' + procid + ''' then
                set frontmost of proc to true
                exit repeat
            end if
        end repeat
    end tell''')

在 Mac OS X 上,PyObjC 提供了一种比使用 osascript 更简洁、更不容易出错的方法:

import os
from Cocoa import NSRunningApplication, NSApplicationActivateIgnoringOtherApps

app = NSRunningApplication.runningApplicationWithProcessIdentifier_(os.getpid())
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)

最近,我在 Mac 上遇到了同样的问题。 我使用@MagerValp for Mac 和@DK for其他系统组合了几个答案:

import platform

if platform.system() != 'Darwin':
    root.lift()
    root.call('wm', 'attributes', '.', '-topmost', True)
    root.after_idle(root.call, 'wm', 'attributes', '.', '-topmost', False)
else:
    import os
    from Cocoa import NSRunningApplication, NSApplicationActivateIgnoringOtherApps

    app = NSRunningApplication.runningApplicationWithProcessIdentifier_(os.getpid())
    app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)

root.mainloop()

某种程度上是各种其他方法的组合,这适用于 OS X 10.11 和在 venv 中运行的 Python 3.5.1,也应该适用于其他平台。 它还通过进程 ID 而不是应用程序名称来定位应用程序。

from tkinter import Tk
import os
import subprocess
import platform


def raise_app(root: Tk):
    root.attributes("-topmost", True)
    if platform.system() == 'Darwin':
        tmpl = 'tell application "System Events" to set frontmost of every process whose unix id is {} to true'
        script = tmpl.format(os.getpid())
        output = subprocess.check_call(['/usr/bin/osascript', '-e', script])
    root.after(0, lambda: root.attributes("-topmost", False))

您在mainloop()调用之前调用它,如下所示:

raise_app(root)
root.mainloop()

当您在 Tkinter._test() 函数中调用 mainloop() 时,有一个关于如何使 Tkinter 窗口成为焦点的提示。

# The following three commands are needed so the window pops
# up on top on Windows...
root.iconify()
root.update()
root.deiconify()
root.mainloop()

这是我发现的最干净、最合适的方法,但只有 Windows 系统需要它。

这个答案是让一个 Tkinter 窗口在其他 Tkinter 窗口的上方弹出。

在我的应用程序中,我有一个大窗口toplevel ,它调用一个小得多的窗口top2 ,它最初出现在toplevel顶部。

如果用户在toplevel窗口内单击,它会获得焦点并阻塞更小的top2窗口,直到toplevel窗口被拖离它为止。

解决方法是点击toplevel的按钮再次启动top2 top2 open 函数知道它已经在运行,所以只需将它提升到顶部并赋予它焦点:

def play_items(self):
    ''' Play 1 or more songs in listbox.selection(). Define buttons:
            Close, Pause, Prev, Next, Commercial and Intermission
    '''

    if self.top2_is_active is True:
        self.top2.focus_force()     # Get focus
        self.top2.lift()            # Raise in stacking order
        root.update()
        return                      # Don't want to start playing again

在 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_idle() .after() 它们都在一种情况下失败:当我直接从 PyCharm 等 IDE 运行我的脚本时,应用程序窗口将留在后面。

我的解决方案适用于我遇到的所有情况。

如果您希望将目标窗口抬起的窗口已知,您可以简单地使用tkraise方法,并将aboveThis参数设置为您想要绘制的窗口。

from tkinter import Tk, ttk, Toplevel

class App(Tk):

    def __init__(self):
        Tk.__init__(self)
        self.title('Main Window')
        self.state('zoomed')
        self.l1 = ttk.Label(self, text='Hello World!')
        self.l1.pack()

        self.s = Splash()
        self.s.tkraise(aboveThis=self)

class Splash(Toplevel):
    def __init__(self):
        Toplevel.__init__(self)
        self.title('Splash Screen')
        self.lift()


app = App()
app.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM