[英]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.