日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python跳转到程序顶部_python-如何使Tkinter窗口跳到最前面?

發(fā)布時間:2024/2/28 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python跳转到程序顶部_python-如何使Tkinter窗口跳到最前面? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python-如何使Tkinter窗口跳到最前面?

如何獲得Tkinter應(yīng)用程序跳轉(zhuǎn)到最前面? 當前,該窗口顯示在我所有其他窗口的后面,并且沒有獲得焦點。

我應(yīng)該打電話給一些方法嗎?

nathan asked 2020-02-14T18:02:57Z

10個解決方案

64 votes

假設(shè)您說“我的其他窗口”是指您的應(yīng)用程序窗口,則可以在頂級或Tk上使用root方法:

root.lift()

如果要使該窗口保持在所有其他窗口之上,請使用:

root.attributes("-topmost", True)

其中root是您的頂級或Tk。 不要忘記"topmost"前面的-!

要使其臨時設(shè)置,請在緊隨其后的最上方禁用:

def raise_above_all(window):

window.attributes('-topmost', 1)

window.attributes('-topmost', 0)

只需將要引發(fā)的窗口作為參數(shù)傳遞即可,這應(yīng)該可行。

D K answered 2020-02-14T18:03:26Z

31 votes

如果在Mac上執(zhí)行此操作,請使用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

關(guān)于Mac,我注意到可能存在一個問題,即如果有多個python GUI運行,則每個進程都將被命名為“ Python”,而AppleScript傾向于將錯誤的進程推向前端。 這是我的解決方案。 這個想法是在加載Tkinter之前和之后獲取正在運行的進程ID的列表。 (請注意,這些是AppleScript進程ID,似乎與posix的ID沒有任何關(guān)系。請參見圖。)然后,奇怪的人將是您的,然后將其移到最前面。 (我認為最后的循環(huán)不是必須的,但是如果您僅獲得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上也遇到了同樣的問題。 我已經(jīng)結(jié)合使用Mac的@MagerValp和其他系統(tǒng)的@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上運行,并且也應(yīng)在其他平臺上運行。 它還通過進程ID而非應(yīng)用名稱來定位應(yīng)用。

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()調(diào)用之前立即調(diào)用它,如下所示:

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運行腳本時,應(yīng)用程序窗口將停留在后面。

我的解決方案適用于遇到的所有情況。

kakyo answered 2020-02-14T18:06:10Z

-1 votes

關(guān)于在Tkinter._test()函數(shù)中調(diào)用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()

這是我發(fā)現(xiàn)的最干凈,最正確的方法,但僅Windows系統(tǒng)才需要。

user2683482 answered 2020-02-14T18:06:34Z

-1 votes

如果知道您想在其上移動目標窗口的窗口,則可以使用tkraise方法來簡單地將aboveThis參數(shù)設(shè)置為您要繪制的窗口。

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

總結(jié)

以上是生活随笔為你收集整理的python跳转到程序顶部_python-如何使Tkinter窗口跳到最前面?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。