我是python的新手。我正在尝试编写一个程序,它会在(x,y)处单击鼠标,将其移动到(a,b)处,然后等待鼠标下面的颜色变成某种颜色,比如#fff。当它是这种颜色时,它会再次单击,然后重复。
我找不到一个好的API来处理python中与鼠标相关的东西。
发布于 2009-12-28 06:03:42
用于模拟鼠标事件的API取决于您的平台。我不知道任何跨平台的解决方案。
在Windows上,你可以通过ctype来访问Win32接口。参见mouse_event on MSDN。您也可能对pywinauto感兴趣
为了获得鼠标下方的颜色,您需要鼠标位置。参见GetCursorPos on MSDN。然后,如果你的应用程序有一个在这个位置获取颜色的API,你可以使用它。如果没有,您可以尝试在光标周围抓取屏幕的一小部分,并使用PIL获取该区域中每个像素的颜色。我认为PIL屏幕截图只适用于Windows paltform,但我不确定。
我使用下面的函数来满足类似的需求:
def grab_main_color(self, rect, max_colors=256):
"""returns a tuple with the RGB value of the most present color in the given rect"""
img=ImageGrab.grab(rect)
colors = img.getcolors(max_colors)
max_occurence, most_present = 0, 0
try:
for c in colors:
if c[0] > max_occurence:
(max_occurence, most_present) = c
return most_present
except TypeError:
raise Exception("Too many colors in the given rect")
https://stackoverflow.com/questions/1967096
复制相似问题