Python 如何随时停止或暂停pyautogui?
我正在使用pyautogui构建一些宏程序 不幸的是,我不能停止循环,所以有时需要花费太多的时间,直到循环结束。 有没有办法在我想的任何时候停止这个节目?或者我只是等到程序结束 下面是我的代码Python 如何随时停止或暂停pyautogui?,python,pandas,database,pyautogui,Python,Pandas,Database,Pyautogui,我正在使用pyautogui构建一些宏程序 不幸的是,我不能停止循环,所以有时需要花费太多的时间,直到循环结束。 有没有办法在我想的任何时候停止这个节目?或者我只是等到程序结束 下面是我的代码 PyAutoGUI具有内置的故障保护功能,可随时终止程序。只需将鼠标移到主显示器的左上角,x,y值为0,0 键入print(pyautogui.FAILSAFE)应返回True,告诉我们故障保护已打开。您还可以通过将其设置为pyautogui.FAILSAFE=False 查看代码时,如果您想一次按多个键
PyAutoGUI具有内置的故障保护功能,可随时终止程序。只需将鼠标移到主显示器的左上角,x,y值为0,0 键入
print(pyautogui.FAILSAFE)
应返回True
,告诉我们故障保护已打开。您还可以通过将其设置为pyautogui.FAILSAFE=False
查看代码时,如果您想一次按多个键,可以使用以下命令节省一些空间:
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()
天哪,这是下一个层次的黑客我想这会帮助你。或者我认为你应该简单地注销你的电脑来停止这个程序。
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()