簡體   English   中英

將光標焦點設置回Python

[英]Setting focus of cursor back to Python

我有一個使用TKinter來打開csv的小程序。 一切正常。

當用戶選擇文件時,我希望光標和活動窗口返回到Python shell。

我正在使用這個:

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

在IDLE中,當程序運行時,它可以工作,但是當我雙擊.py文件並在Python Shell中運行它時,它說找不到路徑。

有人知道我需要的路嗎?

謝謝,

嘗試此操作,它通過pid指代正在運行的進程,因此無論如何運行都無關緊要:

import os
pid = os.getpid()
os.system("""/usr/bin/osascript -e 'tell application "System Events" to set frontmost of (first process whose unix id is %d) to true'""" % pid)

進一步的研究,這是我的解決方案。

import win32gui as wg
from win32gui import GetWindowText, GetForegroundWindow

#This gets the details of the current window, the one running the program
aw = (GetForegroundWindow())

#Do some stuff..

#This tells the program to set the focus on the captured window
wg.SetForegroundWindow(aw)

我希望這可以幫助其他人尋找與我相同的事物。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM