简体   繁体   中英

Setting focus of cursor back to Python

I have a little program that uses TKinter to open a csv. All works fine.

When the user chooses a file, I want the cursor and the active window to return to the Python shell.

I am using this:

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

When in IDLE, this works, when the program runs, but when I just double click the .py file and run it in the Python Shell, it says it can't find the path.

Anyone know the path I need?

Thanks,

Try this, it refers to the running process via pid so it shouldn't matter exactly how you ran it:

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)

Further research and this is my solution.

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)

I hope this helps anyone else looking for the same thing I was. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM