我想制作一个程序,当我按Q时,它将截取屏幕截图并保存。 它可以检测到按键,但不运行其他代码。
a = input()
b = ''
import time
import pyautogui
import PIL
import keyboard # Using module keyboard
while True: # making a loop
try:
if keyboard.is_pressed('q'): # if key 'q' is pressed
print('Detected')
a+=1
im = pyautogui.screenshot()
b = ''
b = str(a)
b+=".jpg"
im.save(b)
print('Success')
time.sleep(1)
else:
pass
except:
b = ''
但是结果只是这样:
Detected
Detected
Detected
Detected
Detected
Detected
Detected
Detected
Detected
Detected
Detected
它只会打印很多“检测到”并且不再运行。