2009-12-12 109 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系統需要的。