python - 如何正确转换列表的输出值以将它们用作函数内的参数?

标签 python python-3.x windows python-imaging-library

我正在尝试用 python 编写一个脚本,它在屏幕上搜索 RGB 中的特定颜色,获取像素坐标,然后将它们发送到单击函数以单击它。到目前为止,这是我的代码:

from PIL import ImageGrab
import win32api, win32con

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

color = (10,196,182)
session = False
match = False

while(True):
    screen = ImageGrab.grab()
    found_pixels = []
    for i, pixel in enumerate(screen.getdata()):
        if pixel == color:
            match = True
            found_pixels.append(i)
            break
    else:
        match = False

    width, height = screen.size
    found_pixels_coords = [divmod(index, width) for index in found_pixels]

    if session == False and match == True:
        click(found_pixels_coords)
        print("Match_Found")
        session = True
    if session == True and match == False:
        session = False

如何转换 founds_pixels_coords 的输出以在 click(x,y) 函数中使用它?我还得到了相反的输出值,(y,x) 而不是 (x,y),我不明白为什么。

这是我的控制台输出,以防我完全错误:

Traceback (most recent call last):
  File "test2.py", line 28, in <module>
    click(found_pixels_coords)
TypeError: click() missing 1 required positional argument: 'y'

编辑: @martineau 建议的 click(*found_pixels_coords[0]) 似乎解决了缺少参数的错误。我还通过定义 click(y,x) 来解决反向值。但是,任何适当的解决方案都将不胜感激。

最佳答案

由于 found_pixels_coords 是一个列表(尽管它永远不会包含多于一组的坐标),因此以下是如何使用其中的一组(如果有匹配的):


    .
    .
    .
    if session == False and match == True:
        click(*found_pixels_coords[0]) # <== Do it like this.
        print("Match_Found")
        session = True
    if session == True and match == False:
        session = False
    .
    .
    .

关于python - 如何正确转换列表的输出值以将它们用作函数内的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56813815/

相关文章:

python - 如何在 Jupyter Lab 中自定义键盘快捷键来运行当前行或选定的文本?

python-3.x - 需要没有复选标记且平坦边框的复选按钮

windows - 在 Windows 7 上安装 Haskell 模块

c++ - CreateFont API 质量

python - 参数里面的冒号是什么意思?

Python 共享属性父/子

python - 根据Python中的多个条件将多个数据帧中的一列合并到另一个数据帧

arrays - 迭代列表中的键值对并转换为 pandas 数据帧

python - 收集python模块中的一些特定类

java - 在第二个驱动器上安装 JDK