0
from PIL import Image
import time
import subprocess
for i in bio:
    p = subprocess.Popen(["C:\Program Files\IrfanView\i_view64.exe",'C:\\Users\Moondra\\Bioteck_charts\{}.png'.format(i)])
    time.sleep(5)
    p.kill()

This is my current code. I'm on Windows 7. I'm iterating through a list, and for each element, I'm opening a picture and then closing it after 5 seconds. However, I would like to be able to pause this loop if I would like to study the image longer and then be able to resume the loop from where I left off.

Psudo-code is something like this:

from PIL import Image
import time
import subprocess
for i in bio:

    p = subprocess.Popen(["C:\Program Files\IrfanView\i_view64.exe",'C:\\Users\Moondra\\Bioteck_charts\{}.png'.format(i)])
    time.sleep(5)
    if Keypress == 'Spacebar': 
        pause
        if Keypress =="Spacebar": resume


    p.kill()

I saw these two threads but one is over 9 years old (on user input) and both seem complicated using threading (which I'm not really familiar with).

User input :

How to get user input during a while loop without blocking

Pausing the loop:

Python - using [spacebar] to pause for loop

Is there a clean way to do what I would like?

EDIT:

Tests using msvcrt

from msvcrt import getch
while True:
    key = ord(getch())
    print(key)
    if key == 27: #ESC
        break

Just seems to output 255 (over and over). If I press esc, no reaction and loop doesn't break. The printed number (255) doesn't change as well despite pressing different keys.

UPDATE:

I'm attempting it a different way. Not sure if this is better but, I found a module called keyboard that is aware of keypresses input (and after playing around with it, it seems to work fine). I'm attempting to use threading to do so, but it seems I'm not getting the timing with threading right. I think the problem is when my time.sleep() is called in my thread, my keypresses aren't read.

from threading import Thread import keyboard import subprocess import pickle

def keyboard_press():
    while p == True:
        if keyboard.is_pressed('down') == True:
            print('yes')
            input()
            p.kill()
        #continue


with open('C:\\Users\Moondra\\Bioteck.pickle', 'rb') as file:
    bio = pickle.load(file)



for i in bio[:5]:

    p = subprocess.Popen(["C:\Program Files\IrfanView\i_view64.exe",'C:\\Users\Moondra\\Bioteck_charts\{}.png'.format(i)])
    from threading import Thread
    t = Thread(target = keyboard_press, args =())
    t.start()
    t.join()
    time.sleep(3)
    p.kill()
Moondra
  • 4,399
  • 9
  • 46
  • 104

1 Answers1

1

You're looking to detect a KeyPress not an Input(), take a look at this for reference

pypypy
  • 1,075
  • 8
  • 18
  • 1
    Thanks. I will update my OP. I looked at the accepted answer in the pasted link; I use Windows 7 so it seems I can't use termios? – Moondra May 25 '17 at 14:04
  • 1
    No you cant, but there is a similar answer for msft https://stackoverflow.com/questions/12175964/python-method-for-reading-keypress – pypypy May 25 '17 at 14:05
  • Thank you. msvcrt doesn't seem to be detecting my key presses. I've updated my OP with my attempt at msvcrt and the response I'm getting. Anything I may be doing wrong? – Moondra May 25 '17 at 15:59