快速鼠标圈的Python脚本运行缓慢,如何加快速度

问题描述 投票:0回答:1

需要使其运行得更快,即/用鼠标非常快地画圈,并循环它直到出现一些预定义的键盘输入。 有人可以帮我解决下面的初始代码吗?

import pyautogui as pg
import math




resolution = pg.size()
radius = 100 # Circle radius
duration = 0.00000000000000000000000000001 # Duration of each move(mouse speed)
steps = 20 # Number of 'moves' or points on the circle

mid_x = resolution[0] / 2
mid_y = resolution[1] / 2

def GetXnY(angle):
    x = radius * math.sin(math.pi * 2 * angle / 360)
    y = radius * math.cos(math.pi * 2 * angle / 360)
    return (x, y)

def DoACircle():
    angle = 0
    while angle  <= 360:
        x, y = GetXnY(angle)
        pg.moveTo((mid_x + x), (mid_y - y), duration = duration)
        angle = angle + (360/steps)
           
DoACircle()

它慢慢地移动鼠标,我需要用键盘中断循环,不知道该怎么做。

python pyautogui
1个回答
0
投票

要提高速度,您需要增加步骤、减少持续时间,并且有更好的方法来优化代码。

优化:

    mid_x = resolution[0] / 2
    mid_y = resolution[1] / 2
    
    def GetXnY(angle):
        x = radius * math.sin(math.pi * 2 * angle / 360)
        y = radius * math.cos(math.pi * 2 * angle / 360)
        return (x, y)
    
    def DoACircle():
        angle = 0
        while angle <= 360:
            x, y = GetXnY(angle)
            pg.moveTo((mid_x + x), (mid_y - y), duration=duration)
            angle += 360 / steps
    
    # Main loop that runs until 'q' is pressed
    while not keyboard.is_pressed('q'):
        DoACircle()

这部分很好,如果你想改变节奏,我单独调整一下。

# Parameters
resolution = pg.size()
radius = 100  # Circle radius
duration = 0  # Duration of each move (mouse speed)
steps = 100  # Number of 'moves' or points on the circle

但是,您需要导入:

import keyboard
© www.soinside.com 2019 - 2024. All rights reserved.