繁体   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