[python-win32] Dealing with windows in Windows

Mike Driscoll mdriscoll at co.marshall.ia.us
Tue Aug 4 16:49:15 CEST 2009


Kimmo Kekkonen wrote:
> Hi!
>
> I were wondering if it is possible to use Python to select a window
> (by title or somehow) and then input text into it? I'd need the trick
> to input text to program I am running from cmd. When program opens it
> also opens one "GUI" window and focus to the cmd will lost. Now I'd
> like to get focus back to cmd and then input text into it. I tried
> PIPEs with Popen but after 6 hours of work it still did not work. I
> think it is because of this my program.
>
> Also I am trying to close active window. Is there a solution to send
> "alt+f4" to currently active window?
There's SendKeys: http://pypi.python.org/pypi/SendKeys/0.3

Or if you know the application will only have one instance running at 
any one time, you can use the sledgehammer approach and do one of the 
following:

from killProcName import killProcName
killProcName(procName)

or

import subprocess
subprocess.Popen('taskkill /f /im evilGUI_process.exe')


Note that the first method presumes that you have PyWin32 installed as 
it is included with that package. On my PC, it's located here: 
C:\Python25\Lib\site-packages\win32\scripts

>
> I managed to do these tricks using WSH but I would like to use Python
> so I would not have to do these "window activations and closing" by
> WSH. Then I will have 100% Python script without any others.
>
> Thanks in advance! 

HTH

- Mike


More information about the python-win32 mailing list