简体   繁体   English

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

[英]How can I properly convert a list's output values to use them as args inside a function?

I'm trying to write a script in python which searches the screen for a specific color in RGB, gets the pixel coordinates and then sends those to a click function to click on it.我正在尝试用 python 编写一个脚本,它在屏幕上搜索 RGB 中的特定颜色,获取像素坐标,然后将它们发送到单击函数以单击它。 Here's my code so far:到目前为止,这是我的代码:

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

How can I convert the output of founds_pixels_coords to use it inside the click(x,y) function?如何转换founds_pixels_coords的输出以在click(x,y)函数中使用它? I'm also getting the output values reversed, (y,x) instead of (x,y) , which I don't understand why.我也得到了反转的输出值, (y,x)而不是(x,y) ,我不明白为什么。

Here's my console output, in case I'm totally wrong:这是我的控制台输出,以防万一我完全错了:

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

EDIT: click(*found_pixels_coords[0]) as suggested by @martineau seems to solve the missing argument error.编辑: click(*found_pixels_coords[0])建议的click(*found_pixels_coords[0])似乎解决了缺少的参数错误。 I also got around the reversed values by defining click(y,x) .我还通过定义click(y,x)绕过了反向值。 Any proper solution to this however, would be greatly appreciated.但是,对此的任何适当解决方案将不胜感激。

Since found_pixels_coords is a list (although it will never contain more than one set of coordindates), here's how to use the one set in it (if any matched):由于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
    .
    .
    .

Just call click() like this (add a * before the list name to unpack the values).只需像这样调用click() (在列表名称前添加一个*以解压缩值)。

click(*found_pixels_coords)  # Make sure list contains 2 values

If you are not sure about number of items in found_pixels_coords list then just change the signature of click() function from如果您不确定found_pixels_coords列表中的项目数,那么只需将click()函数的签名从

def click(x, y):

to

def click(x, y, *args):

In this case, if there will be more than 2 values in list, no problem, 3 to onwards will be in args tuple.在这种情况下,如果列表中有 2 个以上的值,没问题,3 到 3 个将在args元组中。

As you say that the order of the coordinates is inverted, we can use two auxiliary variables to unpack the values and then pass them in the required order:正如你所说的坐标顺序颠倒了,我们可以使用两个辅助变量来解包值,然后按要求的顺序传递它们:

y, x = found_pixels_coords
click(x, y)

If found_pixels_coords is a list with several coordinates, then you can pick the first one with:如果found_pixels_coords是一个包含多个坐标的列表,那么您可以选择第一个:

y, x = found_pixels_coords[0]
click(x, y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确使用 args 或 kwargs 来将它们用作 function 中的回归量输入变量? - How do I use args or kwargs for dicts properly to use them as Regressor input variables in function? 如何在列表中转换我的 function output? - How can I convert my function output in List? 如何使用 function args 来定义变量 - How can I use function args to define a variable 如何使用 `return` 从 for 循环中取回多个值? 我可以把它们放在一个列表中吗? - How can I use `return` to get back multiple values from a for loop? Can I put them in a list? 如何在函数中使用* args返回字典列表? - How to use *args in a function to return a list of dictionaries? 在Python的另一个函数中使用函数时,是否可以隐藏函数的某些返回值? - Can I hide some of my function's return values when I use it inside another function in Python? 我可以使用 inspect.signature 将装饰器的 (*args, **kwargs) 解释为装饰函数的变量吗? - Can I use inspect.signature to interpret a decorator's (*args, **kwargs) as the decorated function's variables would be? 在带有参数的列表中插入函数并使用它们 - Insert function inside list with arguments and use them 给定两个字符串列表,如何将它们转换为dict? - Given two list of strings, how can I convert them into a into a dict? 如何在函数中使用* args? - How to use *args in a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM