4

How to bring appliation (such as Notepad) in front forcedly with 'subprocess' module of Python on MS Windows?

Environment: Windows XP, Python 2.5.4

I tried the followings.

## Notepad is invoked in front
>>> import subprocess
>>> command = r'notepad C:\tmp\foo.txt'
>>> subprocess.Popen(command) # opened in front

## Notepad is invoked but not in front
>>> import subprocess, time
>>> command = r'notepad C:\tmp\foo.txt'
>>> time.sleep(5)    # click other application while sleeping
>>> subprocess.Popen(command) # opened but not in front!

I want notepad window to be in front forcedly even when other application is selected.

kwatch
  • 508
  • 1
  • 5
  • 12
  • http://stackoverflow.com/questions/1482565/how-to-make-python-window-run-as-always-on-top – user225312 Nov 20 '10 at 07:43
  • @sukhbir: that thread deals with making the Python window on top; in this case it's useless until he can get the window ID, so I think we can consider *that* to be the problem. – Chris Morgan Nov 20 '10 at 12:46
  • To do this you'll need the `hWnd` or Windows ID of the process you started. I found this article titled [*Find the window for my subprocess*](http://timgolden.me.uk/python/win32_how_do_i/find-the-window-for-my-subprocess.html) that explains how to do that in Python. After getting the `hWnd` you'll need to make some calls to the Win32 API to bring the Window to the front. Google is your friend. – martineau Dec 14 '10 at 19:32
  • martineau is correct. You could probably look inside pywinauto.controls.HwndWrapper.SetFocus() and find code you could use – markm Oct 10 '11 at 05:24

0 Answers0