首页 > 解决方案 > python在执行时关闭 pyautogui.press('enter')

问题描述

我正在制作一个可以打字的机器人,但是当它按下输入时,程序在未完成时关闭

import sched, time, pyautogui
s = sched.scheduler(time.time, time.sleep)
def do_something(sc): 
    pyautogui.write('hello')
    pyauogui.press('enter')
    s.enter(30, 1, do_something, (sc,))

s.enter(5, 1, do_something, (s,))
s.run()

我在论坛上找到了这段代码来创建一个循环,该循环在重复之前等待 x 次,但是当涉及到 pyauogui.press('enter') 行时它会关闭,这是为什么?

标签: pythonpyautogui

解决方案


I ran your code and got the same issue, then I looked at it and in line 5 you have a type "pyaugui.press('enter')" as you may guess by now it is spelt pyautogui as seen in the line above and in your import statement.

# Fixed Code
import sched, time, pyautogui
s = sched.scheduler(time.time, time.sleep)
def do_something(sc):
    pyautogui.write('hello')
    # Your type was here
    pyautogui.press('enter')
    s.enter(30, 1, do_something, (sc,))

s.enter(5, 1, do_something, (s,))
s.run()

I've tested and it now runs. I can recommend getting a IDE or text editor with built in error highlighting.

Examples:

Visual studio code (Microsoft) - https://code.visualstudio.com/

Visual studio (Microsoft) - https://visualstudio.microsoft.com/

PyCharm (Jetbrains) https://www.jetbrains.com/pycharm/

All of these are free (PyCharm has a paid version)


推荐阅读