pyautogui位置(Mac和Windows)

发布于 2025-02-09 22:07:11 字数 943 浏览 3 评论 0原文

当我运行代码时,我会面临一些问题。 我正在Mac和Windows中开发相同的代码。

该函数从Pyautogui定位,即使我安装了OpenCV,也无法在Windows中工作。 Mac中的相同图像完美地

创建了一个功能,以了解该代码是否在Windows或Mac中运行,仅仅是因为分辨率

看到下面的代码

    cond = True
    while cond:
        try:
            x, y = auto.center(auto.locateOnScreen(settings.btnLogin, confidence=.5))
            ## Click in Windows
            auto.click(x, y)

            ## Click in Mac
            auto.click(x/2, y/2)
            cond = False
        except Exception as e:
            print("Error", e)
    return

,我只是无法理解它在Mac和Windows中工作的原因,而Windows则不断返回none none

有人可以帮助我吗?

Im facing some problems when i run my code.
Im developing the same code in Mac and in Windows.

The function LocateOnScreen from PyAutoGui is not working in Windows, even I installed OpenCV. The same image in MAC works perfetctly

I created a funtion to know if the code is running in Windows or Mac, just because the resolution

See the code below

    cond = True
    while cond:
        try:
            x, y = auto.center(auto.locateOnScreen(settings.btnLogin, confidence=.5))
            ## Click in Windows
            auto.click(x, y)

            ## Click in Mac
            auto.click(x/2, y/2)
            cond = False
        except Exception as e:
            print("Error", e)
    return

I just cant understand why it works in Mac and Windows keep returning None

Login Button
Full page

Anybody can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秉烛思 2025-02-16 22:07:11

所以我想到了:

  1. 请确保您的图像大小与图像相同
    您正在尝试使用机器人找到。

  2. 将其放在与“ .py”文件的同一文件夹中
    “ btnlogin.jpg”。

工作代码应该是这样的:

import pyautogui
cond = True

while cond:
    try:
        x, y = pyautogui.locateCenterOnScreen('btnLogin.jpg')
        #Click in Windows
        pyautogui.click(x, y)
        cond = False
    except Exception as Ex:
        print('Error: ', Ex)

这基本上是代码的核心,不是循环的,但是如果您只需要自动登录,则应该足够快:

import pyautogui
try:
    x, y = pyautogui.locateCenterOnScreen('btnLogin.jpg')
    pyautogui.click(x, y)
except Exception as Ex:
    print('ERROR: ', Ex)

让我知道是否某物不清楚或对您不起作用:)

So I came up with this:

  1. Make sure your image is the same size as the one
    you're trying to find with your bot.

  2. Put it in the same folder as the ".py" file as
    "btnLogin.jpg".

The working code should be like this:

import pyautogui
cond = True

while cond:
    try:
        x, y = pyautogui.locateCenterOnScreen('btnLogin.jpg')
        #Click in Windows
        pyautogui.click(x, y)
        cond = False
    except Exception as Ex:
        print('Error: ', Ex)

This is basically the core of the code, doesn't cycle but should be fast enough if you just need to automate a login:

import pyautogui
try:
    x, y = pyautogui.locateCenterOnScreen('btnLogin.jpg')
    pyautogui.click(x, y)
except Exception as Ex:
    print('ERROR: ', Ex)

Let me know if something isn't clear or doesn't work for you :)

痴情 2025-02-16 22:07:11

我设置了以预设尺寸打开的窗口,因此我拿了所有需要的印刷品。

我检查一下是否正在找到所有图像,然后发现它们正确。但是现在我将其运行在Mac上,以相同大小的窗口打开窗口,但是,它找不到图像。

我不知道为什么,但是即使有相同的窗口尺寸,Mac上的图像也会较小:(

我打开了一个大小(1440,900)的窗口,并拍摄了2张登录按钮的图片。其他

大小。

  • 图片

I set the window to open at a preset size, so I took all the prints I needed.

I checked to see if I was finding all the images, and I was finding them just right. But now I put it to run on Mac, to open the window with the same size, however, it doesn't find the images.

I don't know why, but even with the same window size, the image appears smaller on Mac :(

I open a window with the size (1440, 900) and took 2 pictures of the Login button. 1 picture in Mac and the other in Windows.

The size of the pictures:

  • Mac = (207, 53)
  • Windows = (253, 73)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文