I don’t like these suggestions of using win32gui
because you can’t easily install that via pip
. So here’s my solution:
First, install pywinauto
via pip
. If you’re on Python 2.7.9 or a newer version on the 2 branch, or Python 3.4.0 or a newer version from the 3 branch, pip
is already installed. For everyone else, update Python to get it (or you can manually download and install it by running this script, if you must run an older version of Python.)
Just run this from the command line (not from within Python):
pip install pywinauto
Next, import what you need from pywinauto
:
from pywinauto.findwindows import find_window
from pywinauto.win32functions import SetForegroundWindow
Finally, it’s just one actual line:
SetForegroundWindow(find_window(title="taskeng.exe"))