将光标焦点设置回Python

时间:2018-11-26 17:42:21

标签: python tkinter os.system

我有一个使用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中运行它时,它说找不到路径。

有人知道我需要的路径吗?

谢谢

2 个答案:

答案 0 :(得分:0)

尝试一下,它指的是通过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)

答案 1 :(得分:0)

进一步研究,这是我的解决方案。

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)

我希望这可以帮助其他人寻找与我相同的事物。 :-)