2009-12-12 106 views

回答

47

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

root.lift() 

如果你想在窗口停留于其他窗口之上,使用:

root.attributes("-topmost", True) 

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

为了让临时,禁用权最顶端后:

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

你想提高作为一个参数的窗口就通了,这应该工作。

+2

试过在mac 10.9上的.lift()没有工作。 – shawn 2013-12-30 16:48:14

+2

我的意思是: windows.attributes(1)的确带到了前面,但(0)似乎没有禁用它。它实际上发送到后面。 – shawn 2013-12-30 16:57:13

+1

在Windows 7中工作 – Aftershock 2014-03-22 19:39:03

27

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

import os 

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

在mac上工作10.9 – shawn 2013-12-30 16:56:14

+1

在小牛上完美工作。非常感谢你。 – edsonlp1 2014-01-09 14:22:27

+2

需要发生在root.mainloop()前 – Joris 2014-12-29 11:52:10

5

关于在Mac,我发现可以有这样的问题:如果有运行多个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''') 
2

在Mac OS X PyObjC提供了一个更清洁,不易出错的方法比脱壳而出,以osascript:

import os 
from Cocoa import NSRunningApplication, NSApplicationActivateIgnoringOtherApps 

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

当关闭窗口时,出现错误:分段错误:11 – metaphy 2015-03-19 14:22:21

+0

它在Tkinter应用程序中运行,没有在10.10.2上使用系统python进行分割。尝试删除代码的其他部分,这可能是其他的崩溃。 – MagerValp 2015-03-20 08:25:39

4

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

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

这是什么编程语言 – zavr 2017-01-31 20:27:18

+0

语言是Python – 2017-01-31 23:06:37

+0

与其他分支(其中没有Cocoa模块)不同,在Sierra上添加和删除'-topmost'。我正在运行OS X默认tk。 – 2017-04-04 17:55:23

12

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

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

它可以完美的我。当窗口生成时,它使窗口到达前端,并且它不会始终保持在前端。

+1

我正在运行10.11这是唯一对我有用的答案。 – user1247 2016-09-02 03:17:12

4

这种方法结合了各种其他方法,它适用于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() 
0

有一个关于如何使Tkinter的窗口获得焦点,当你调用在Tkinter._test主循环()的提示( )功能。

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

这是我找到的最干净最正确的方式,但它只是Windows系统需要的。