首页 > 解决方案 > 将光标的焦点设置回 Python

问题描述

我有一个使用 TKinter 打开 csv 的小程序。一切正常。

当用户选择一个文件时,我希望光标和活动窗口返回到 Python shell。

我正在使用这个:

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

当处于空闲状态时,这在程序运行时有效,但是当我双击 .py 文件并在 Python Shell 中运行它时,它说找不到路径。

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

谢谢,

标签: pythontkinteros.system

解决方案


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

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)

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


推荐阅读