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()