[英]pressing “spacebar” to stop a pause a loop, press again to resume loop (see UPDATE)
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.
我在Windows 7上。我正在遍历列表,对于每个元素,我正在打开图片,然后在5秒钟后关闭它。 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). 我看到了这两个线程,但是其中一个已经使用了9年(根据用户输入),并且使用线程似乎都很复杂(我不太熟悉)。
User input : 用户输入:
How to get user input during a while loop without blocking 如何在while循环中获得用户输入而不会阻塞
Pausing the loop: 暂停循环:
Python - using [spacebar] to pause for loop Python-使用[空格键]暂停循环
Is there a clean way to do what I would like? 有没有做我想做的干净方法?
EDIT: 编辑:
Tests using msvcrt
使用
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). 似乎只输出255(一遍又一遍)。 If I press
esc
, no reaction and loop doesn't break. 如果按
esc
,则任何反应和循环都不会中断。 The printed number (255) doesn't change as well despite pressing different keys. 尽管按了不同的键,打印的数字(255)也没有改变。
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). 不知道这是否更好,但是,我找到了一个名为
keyboard
的模块,该模块可以识别按键输入(并在使用它后似乎工作正常)。 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.
我认为问题是当在线程中调用time.sleep()时,无法读取我的按键。
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()
您想检测一个按键不是输入(),看看这个以供参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.