1

I wrote a program that downloads images from urls using urllib.urlretrieve( url , address ).

After each download of image, I open it using os.startfile(address) or os.system(address).

Then I wait for 5 seconds using time.sleep(5) and then download next image and open it in a same way.

It opens, but the old one is also still opened. I want the old window to be closed.

One way would be to some how tell python 2.7 to simulate alt+F4 in order to close the old window before opening the new one.

Ryne Everett
  • 6,427
  • 3
  • 37
  • 49
  • Use `os.kill`. If earlier than 2.7/3.2, use `ctypes` with `kernel32.TerminateProcess`. – Brian Cain Oct 10 '14 at 17:40
  • @BrianCain: How would that work? `os.startfile` doesn't give you a PID. Even if it did, there's a good chance all those windows are owned by the same process (which may well have other windows open too). – abarnert Oct 10 '14 at 17:46
  • http://stackoverflow.com/questions/13327759/how-to-display-and-close-image-in-python –  Oct 10 '14 at 17:48
  • How do you know what "the window" is? What if another window gets opened during those 5 seconds? What if one of the files fails to open a window? What if the user's default .JPG app doesn't open a new window for each file? If you can't come up with a rule that clearly and unambiguously identifies the window you want to close, you can't write code to do so. – abarnert Oct 10 '14 at 17:49
  • 1
    A better solution would be to script some particular program (e.g., via its COM automation interface) instead of just telling Windows to "open this image with whatever program it wants to" and then trying to guess what happened after the fact. Alternatively, you could use, e.g., Tkinter+PIL, or pywin32, to display your own image windows instead of using a separate app at all. – abarnert Oct 10 '14 at 17:51
  • Have a look at this question, you could use Tk to show a window that you can control. http://stackoverflow.com/questions/16465591/how-can-you-display-an-image-on-a-gui-using-tk-in-python2-7 – Rod Oct 10 '14 at 20:16

0 Answers0