如何使Tkinter窗口跳到前面?

我如何得到一个Tkinter应用程序跳转到前面? 目前,窗口出现在所有其他窗口后面,并没有得到重点。

有什么方法我应该打电话?

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

 root.lift() 

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

 root.attributes("-topmost", True) 

root是你的Toplevel还是Tk。 不要忘记"topmost"面前!

要使其暂时停用,请在以下位置禁用最上层:

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

只要通过你想提出的窗口作为参数,这应该工作。

如果你在Mac上这样做,请使用AppleEvents将焦点集中到Python。 例如:

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

在主循环()之前添加以下行:

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

这对我来说是完美的。 当窗口产生时,它使窗口到达前面,而不会始终保持在前面。

关于Mac,我注意到可能有一个问题,如果有多个python GUI运行,每个进程将被命名为“Python”,而AppleScript将倾向于将错误的命名推到前面。 这是我的解决scheme。 这个想法是在加载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''') 

这个方法有许多其他方法的组合,它们适用于OS X 10.11和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() 

最近,我在Mac上也有同样的问题。 我已经结合了几个答案使用@MagerValp为Mac和@DK为其他系统:

 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() 

在Mac OS X上PyObjC比osascript提供了一个更简洁易用的方法:

 import os from Cocoa import NSRunningApplication, NSApplicationActivateIgnoringOtherApps app = NSRunningApplication.runningApplicationWithProcessIdentifier_(os.getpid()) app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps) 

在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() 

这是我find的最干净的方法,但是只有Windows系统才需要。