# Moving the mouse in a circle

Hi all,

I’m pretty new to Python, and I’m really struggling to programmatically get the mouse to move in a circular motion.

I have found countless articles about how to draw circles, but I don’t want to draw anything, I just need the mouse to move.

I have found one code example that is actually doing the task I want it to, however this code takes 40 seconds to execute. I need this circle to be completed in under a second.

``````import pyautogui
import math

R = 400
# measuring screen size
(x,y) = pyautogui.size()
# locating center of the screen
(X,Y) = pyautogui.position(x/2,y/2)
pyautogui.moveTo(X+R,Y)

for i in range(360):
# setting pace with a modulus
if i%6==0:
``````

Does anyone know how I can speed this up?

Many thanks!

What a weird way of implementing an iteration step. This way is more natural and efficient (just the loop):

``````STEP = 6

for i in range(0, 360, STEP):
``````

I would also de-duplicate the code and improve readability:

``````STEP = 6

for angle_deg in range(0, 360, STEP):
pyautogui.moveTo(
``````

I guess that the call `pyautogui.moveTo()` takes a lot of time (about 0.7 seconds per single call?). Try to increase the `STEP`. …or maybe there is a different library which is able to position the mouse cursor faster.

1 Like

Ah thank you, that’s brilliant! I increased the steps to 60 and it seems to be exactly the speed I’m looking for.

It’s not a perfect circle but its enough to do the job!

I wanted to see what could be done with a tkinter canvas. It cannot move the user mouse pointer, but it can move anything one can draw, include unicode characters.

``````from math import *
from time import sleep
from tkinter import *

root = Tk()
can = Canvas(root, width=1000, height=1000)
can.pack()
x0, y0, r, steps = 500, 500, 400, 360
x, y = x0+r, y0
wid = can.create_text(x, y, text='x')
can.update()
With `sleep(.05)`, it takes about 16 seconds. Without, it is too fast to see the movement. A proper tkinter program would use `mainloop` and `after` callbacks instead of `update` and `for`. Delays could then by specified in milliseconds.