python跳转到程序顶部_python-如何使Tkinter窗口跳到最前面?
python-如何使Tkinter窗口跳到最前面?
如何獲得Tkinter應用程序跳轉到最前面? 當前,該窗口顯示在我所有其他窗口的后面,并且沒有獲得焦點。
我應該打電話給一些方法嗎?
nathan asked 2020-02-14T18:02:57Z
10個解決方案
64 votes
假設您說“我的其他窗口”是指您的應用程序窗口,則可以在頂級或Tk上使用root方法:
root.lift()
如果要使該窗口保持在所有其他窗口之上,請使用:
root.attributes("-topmost", True)
其中root是您的頂級或Tk。 不要忘記"topmost"前面的-!
要使其臨時設置,請在緊隨其后的最上方禁用:
def raise_above_all(window):
window.attributes('-topmost', 1)
window.attributes('-topmost', 0)
只需將要引發的窗口作為參數傳遞即可,這應該可行。
D K answered 2020-02-14T18:03:26Z
31 votes
如果在Mac上執行此操作,請使用AppleEvents將焦點集中在Python上。 例如:
import os
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
username answered 2020-02-14T18:03:46Z
30 votes
在mainloop()之前添加以下行:
root.lift()
root.attributes('-topmost',True)
root.after_idle(root.attributes,'-topmost',False)
它非常適合我。 生成窗口時,它使窗口位于最前面,并且不會一直保持在最前面。
user6107173 answered 2020-02-14T18:04:10Z
5 votes
關于Mac,我注意到可能存在一個問題,即如果有多個python GUI運行,則每個進程都將被命名為“ Python”,而AppleScript傾向于將錯誤的進程推向前端。 這是我的解決方案。 這個想法是在加載Tkinter之前和之后獲取正在運行的進程ID的列表。 (請注意,這些是AppleScript進程ID,似乎與posix的ID沒有任何關系。請參見圖。)然后,奇怪的人將是您的,然后將其移到最前面。 (我認為最后的循環不是必須的,但是如果您僅獲得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''')
Ted C answered 2020-02-14T18:04:32Z
4 votes
在Mac OS X上,PyObjC提供了一種比osascript更干凈,更不易出錯的方法:
import os
from Cocoa import NSRunningApplication, NSApplicationActivateIgnoringOtherApps
app = NSRunningApplication.runningApplicationWithProcessIdentifier_(os.getpid())
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)
MagerValp answered 2020-02-14T18:04:52Z
4 votes
最近,我在Mac上也遇到了同樣的問題。 我已經結合使用Mac的@MagerValp和其他系統的@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()
Tomasz Nguyen answered 2020-02-14T18:05:12Z
4 votes
它是多種其他方法的某種組合,可在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()
Caleb Hattingh answered 2020-02-14T18:05:36Z
0 votes
在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('', OnFocusIn)
想法是將其帶到最前端,直到用戶與其交互,即集中精力。
我嘗試了接受的答案.after_idle()和.after()。它們都在一種情況下失敗:當我直接從PyCharm之類的IDE運行腳本時,應用程序窗口將停留在后面。
我的解決方案適用于遇到的所有情況。
kakyo answered 2020-02-14T18:06:10Z
-1 votes
關于在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系統才需要。
user2683482 answered 2020-02-14T18:06:34Z
-1 votes
如果知道您想在其上移動目標窗口的窗口,則可以使用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()
Sourav B. Roy answered 2020-02-14T18:06:55Z
總結
以上是生活随笔為你收集整理的python跳转到程序顶部_python-如何使Tkinter窗口跳到最前面?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 600内存超级赛车游戏,画质流畅操作体验
- 下一篇: 4GB内存玩游戏,够用还省钱