如何使我的鼠标在设置框中随机单击?

时间:2019-07-15 03:38:30

标签: python winapi python-imaging-library

我试图让鼠标沿着程序内的栏随机单击以设置数据。我不确定该怎么做。我尝试在底部的leftClick内设置一个框,但这不足为奇。我是python的新手,我正在参加我所参加的航空工程课程。

from PIL import ImageGrab
import os
import time
import win32api, win32con
import random

# Globals
# ----------------

x_pad = 14
y_pad = 70

def screenGrab():
    box = (x_pad+1,y_pad+1,x_pad+1508,y_pad+258)
    im = ImageGrab.grab(box)
    im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) + '.png', 'PNG')

def main():
    screenGrab()

if __name__ == '__main__':
    main()

"""
Omitted code not necessary for this part
"""

def get_cords():
    x,y = win32api.GetCursorPos()
    x = x - x_pad
    y = y - y_pad

def startGame():
    #location of first bar
    mousePos((33,125))
    leftClick(

2 个答案:

答案 0 :(得分:0)

类似于@PeptideWitch,但是在鼠标事件中更多的“ Python”。

from pynput.mouse import Button, Controller
import random

def click_box(x_min, x_max, y_min, y_max):
    new_x = random.uniform(x_min, x_max)
    new_y = random.uniform(y_min, y_max)
    print(new_x,new_y)
    mouse = Controller()
    mouse.position = (new_x, new_y)
    mouse.click(Button.left, 1)

答案 1 :(得分:-1)

看看python's inbuilt random.randrange,它返回您指定坐标之间的值,例如(33和124)。您将需要输入框的外部限制,然后让python在这两个空格之间返回一个随机值。

然后,尝试something like this answer

def click_box(x_min, x_max, y_min, y_max):
    new_x = random.randrange(x_min, x_max+1)
    new_y = random.randrange(y_min, y_max+1)
    win32api.SetCursorPos((new_x,new_y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,new_x,new_y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,new_x,new_y,0,0)