【问题标题】:Pyautogui: I have to move the mouse manually for pyautogui to clickPyautogui:我必须手动移动鼠标才能让 pyautogui 点击
【发布时间】:2021-04-03 14:30:39
【问题描述】:

注意:我的问题不是它不点击,我的问题是它只有在我手动移动鼠标后才会点击

我正在使用 python 库“pyautogui”,当我运行这样的代码时:

import pyautogui
pyautogui.FAILSAFE = True
while True:
    cash = pyautogui.locateOnScreen('cash.png',confidence=0.8)
    if cash != None:
        pyautogui.click(cash)

它找到了它正在寻找的东西,但没有点击它直到我手动移动鼠标。

每次我尝试 pyautogui 时都会发生这种情况。

【问题讨论】:

  • 我尝试在 Windows 10 上使用 PyCharm 2020.3 和 Python 3.9 运行您的代码,它运行良好。尝试在pyautogui.click(cash) 之后添加time.sleep(1),我在想程序可能在一秒钟内点击太频繁?
  • 附带说明,pyautogui.FAILSAFE 默认为True,因此您不需要该行
  • @AndrewStone 我知道故障保护是默认开启的,我只是想确定一下,它确实有效!
  • 太棒了!很高兴我能帮上忙

标签: python pyautogui


【解决方案1】:

我会做的不是去

import pyautogui
pyautogui.FAILSAFE = True
while True:
    cash = pyautogui.locateOnScreen('cash.png',confidence=0.8)
    if cash != None:
        pyautogui.click(cash)

你会的

import pyautogui
pyautogui.FAILSAFE = True
while True:
    cash = pyautogui.locateOnScreen('cash.png',confidence=0.8)
    if cash != None:
    pyautogui.move(cash)
        pyautogui.click(cash)

你的问题是程序只是要运行点击,但它不知道实际移动到那里然后点击。

【讨论】:

    【解决方案2】:

    Pyautogui 似乎发送了太多请求让鼠标处理,因此通过添加 time.sleep(1) 来减慢它的速度就可以了。

    我不知道其他人是否有这个问题,但我很高兴它得到了解决!

    【讨论】:

      【解决方案3】:

      这可能是因为您没有在“click()”函数中插入坐标。如果您不插入坐标,python 将单击鼠标指针所在的位置。如果您插入诸如“click(12, 35)”之类的坐标,那么 python 将单击您输入的特定坐标。您可以通过任何图像编辑器(如绘画)获取屏幕坐标

      【讨论】:

      • 你解释了情况,但不是解决方案。
      • 看,如果你只输入click(),那么python会点击你鼠标指针所在的位置。但是如果你告诉python点击某个x坐标和y坐标比如click((x position)12,(y position)35),那么python会点击你输入的位置标记。如果您仍有疑问,请询问
      • 让我们看一下您的代码:import pyautogui pyautogui.FAILSAFE = True while True: cash = pyautogui.locateOnScreen('cash.png',confidence=0.8) if cash != None: pyautogui.click(cash) 所以,您可以在绘图中打开cash.png,在照片编辑器中获取 xy 坐标并将其粘贴到click() 函数如下:import pyautogui pyautogui.FAILSAFE = True while True: cash = "Your x co-ordinates", "Your y co-ordinates" if cash != None: pyautogui.click(cash)
      【解决方案4】:
      >>> import pyautogui
      
      >>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.
      
      >>> currentMouseX, currentMouseY = pyautogui.position() # Get the XY position of the mouse.
      
      >>> pyautogui.moveTo(100, 150) # Move the mouse to XY coordinates.
      
      >>> pyautogui.click()          # Click the mouse.
      >>> pyautogui.click(100, 200)  # Move the mouse to XY coordinates and click it.
      >>> pyautogui.click('button.png') # Find where button.png appears on the screen and click it.
      
      >>> pyautogui.move(0, 10)      # Move mouse 10 pixels down from its current position.
      >>> pyautogui.doubleClick()    # Double click the mouse.
      >>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)  # Use tweening/easing function to move mouse over 2 seconds.
      
      >>> pyautogui.write('Hello world!', interval=0.25)  # type with quarter-second pause in between each key
      >>> pyautogui.press('esc')     # Press the Esc key. All key names are in pyautogui.KEY_NAMES
      
      >>> pyautogui.keyDown('shift') # Press the Shift key down and hold it.
      >>> pyautogui.press(['left', 'left', 'left', 'left']) # Press the left arrow key 4 times.
      >>> pyautogui.keyUp('shift')   # Let go of the Shift key.
      
      >>> pyautogui.hotkey('ctrl', 'c') # Press the Ctrl-C hotkey combination.
      
      >>> pyautogui.alert('This is the message to display.') # Make an alert box appear and pause the program until OK is clicked.
      

      在你的情况下使用 pyautogui.click(100, 200) # Move the mouse to XY

      【讨论】:

        猜你喜欢
        • 2021-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 2019-04-13
        • 2022-11-18
        • 2021-06-16
        相关资源
        最近更新 更多