how to give focus to another application

Mike Driscoll kyosohma at gmail.com
Wed Jun 25 14:43:50 EDT 2008


On Jun 25, 1:23 pm, "massimo s." <deviceran... at gmail.com> wrote:
> Hi,
>
> I would like to know if 1)there is a Python way to tell under which
> terminal process a Python command-line application is running 2)there
> is a Python way to tell the OS to give focus to another window.
>
> Solutions for Windows, Linux and OS X are welcome, even if OS-specific
> (of course general solutions are better, but I can kludge & check the
> platform).
>
> Thanks,
> M.

On Windows, you can use the pywin32 modules to assist you. There's a
sub-module in them called win32gui that has a SetForegroundWindow
method. I usually use win32gui.EnumWindows() to loop through all the
open processes and then use both win32gui.ShowWindow() and
win32gui.SetForegroundWindow() to make the window I want appear.

I'm guessing that each OS has their own methods for this as you would
almost certainly have to use some kind of low-level calls to get
handles on processes you didn't create. Thus, the likelihood that
there are cross-platform methods is pretty low.

-------------------
Mike Driscoll

Blog:   http://blog.pythonlibrary.org
Python Extension Building Network:     http://www.pythonlibrary.org



More information about the Python-list mailing list