python - 如何在我想要的任何时候停止或暂停 pyautogui?
问题描述
我正在使用 pyautogui 构建一些宏程序。
不幸的是,我无法停止 for 循环,所以有时直到 for 循环结束需要很长时间。有什么方法可以在我想要的任何时候停止程序吗?或者我只是等到程序结束。
下面是我的代码
CURRENT_DIR = os.getcwd()
list = os.path.join(CURRENT_DIR,'BOM_list.xlsx')
df = pd.read_excel(list)
for i in df['MLFB'].index:
MLFB = df.loc[i, 'MLFB']
print(MLFB)
a = pyautogui.locateCenterOnScreen('a_material.png')
print(a)
pyautogui.moveTo(a)
pyautogui.moveRel(350, 0)
pyautogui.click()
pyautogui.keyDown('ctrl')
pyautogui.press('a')
pyautogui.keyUp('ctrl')
pyautogui.typewrite(MLFB)
b = pyautogui.locateCenterOnScreen('b_execute.png')
print(b)
pyautogui.moveTo(b)
pyautogui.click()
time.sleep(2.5)
pyautogui.keyDown('alt')
pyautogui.press('y')
pyautogui.press('t')
pyautogui.press('a')
pyautogui.press('i')
pyautogui.keyUp('alt')
time.sleep(2)
pyautogui.press('down')
pyautogui.typewrite(['enter'])
time.sleep(2)
c = pyautogui.locateCenterOnScreen('c_Directory.png')
pyautogui.moveTo(c)
pyautogui.moveRel(350, 0)
pyautogui.click()
pyautogui.keyDown('ctrl')
pyautogui.press('a')
pyautogui.keyUp('ctrl')
pyautogui.typewrite(CURRENT_DIR)
pyautogui.click()
time.sleep(1.5)
d = pyautogui.locateCenterOnScreen('d_Filename.png')
pyautogui.moveTo(d)
pyautogui.moveRel(350, 0)
pyautogui.click()
pyautogui.keyDown('ctrl')
pyautogui.press('left')
pyautogui.keyUp('ctrl')
pyautogui.typewrite(MLFB)
time.sleep(0.5)
pyautogui.typewrite(['enter'])
time.sleep(2)
e = pyautogui.locateCenterOnScreen('e_go_back.png')
pyautogui.moveTo(e)
pyautogui.click()
time.sleep(2)
解决方案
PyAutoGUI 具有内置的故障保护功能,可以随时终止程序。只需将鼠标移动到主显示器的左上角,您的 x、y 值将为 0、0。
打字print(pyautogui.FAILSAFE)
应该返回True
,告诉我们故障保护已打开。如果它妨碍您的程序,您也可以将其设置为pyautogui.FAILSAFE = False
查看您的代码,当您想一次按多个键时,可以使用hotkey()节省一些空间:
pyautogui.keyDown('ctrl')
pyautogui.press('a')
pyautogui.keyUp('ctrl')
是相同的:
pyautogui.hotkey('ctrl', 'a')
您还可以查看线程,它允许您一次运行多个进程。
以下代码将运行一个示例主程序,当Esc
按下该键时,主程序将暂停,并提示用户是否要继续。
import time
from threading import Thread
from pynput import keyboard
def exit_program():
def on_press(key):
if str(key) == 'Key.esc':
main.status = 'pause'
user_input = input('Program paused, would you like to continue? (y/n) ')
while user_input != 'y' and user_input != 'n':
user_input = input('Incorrect input, try either "y" or "n" ')
if user_input == 'y':
main.status = 'run'
elif user_input == 'n':
main.status = 'exit'
exit()
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
def main():
main.status = 'run'
while True:
print('running')
time.sleep(1)
while main.status == 'pause':
time.sleep(1)
if main.status == 'exit':
print('Main program closing')
break
Thread(target=main).start()
Thread(target=exit_program).start()
推荐阅读
- c++ - Unreal Engine 3 game crashes with Steam Input
- rest - automatching property_path in Symfony API
- c# - 无法从 NodeJS 连接到 (LocalDb)\\MSSQLLocalDB
- json - Parse JSON String with TSQL
- android - BiometricPrompt 虹膜和面部提示不适用于加密对象身份验证。#AndroidX
- flutter - Why the Id of object addedItem is always return 1 until I refresh page
- python - Matching strings with multiple lists for a search function
- java - How to refactor methods that only differ in which method they call on an object in java?
- python - Python函数中的HTML代码不起作用
- automated-tests - 如何将来自 javascript 的 karate.log 调用添加到黄瓜报告中?