# Moving the mouse in a circle

**URL:** <https://discuss.python.org/t/moving-the-mouse-in-a-circle/20493>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 28, 2022, 2:05pm UTC](https://discuss.python.org/t/moving-the-mouse-in-a-circle/20493 "2022-10-28T14:05:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PrizeGotti](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/prizegotti/32/9565_2.png) [@PrizeGotti](https://discuss.python.org/u/PrizeGotti)\
**Post date:** [October 28, 2022, 2:05pm UTC](https://discuss.python.org/t/moving-the-mouse-in-a-circle/20493/1 "2022-10-28T14:05:03Z")

</div>

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.

```auto
import pyautogui
import math

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

for i in range(360):
    # setting pace with a modulus 
    if i%6==0:
       pyautogui.moveTo(X+R*math.cos(math.radians(i)),Y+R*math.sin(math.radians(i)))

```

Does anyone know how I can speed this up?

Many thanks!

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 28, 2022, 2:43pm UTC](https://discuss.python.org/t/moving-the-mouse-in-a-circle/20493/2 "2022-10-28T14:43:50Z")

</div>

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

```auto
STEP = 6

for i in range(0, 360, STEP):
    pyautogui.moveTo(X+R*math.cos(math.radians(i)),Y+R*math.sin(math.radians(i)))

```

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

```auto
STEP = 6

for angle_deg in range(0, 360, STEP):
    angle_rad = math.radians(angle_deg)
    pyautogui.moveTo(
            X + R * math.cos(angle_rad),
            Y + R * math.sin(angle_rad))

```

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.

---

<div class="post-metadata">

**Author:** ![PrizeGotti](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/prizegotti/32/9565_2.png) [@PrizeGotti](https://discuss.python.org/u/PrizeGotti)\
**Post date:** [October 28, 2022, 2:58pm UTC](https://discuss.python.org/t/moving-the-mouse-in-a-circle/20493/3 "2022-10-28T14:58:24Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [October 28, 2022, 8:52pm UTC](https://discuss.python.org/t/moving-the-mouse-in-a-circle/20493/4 "2022-10-28T20:52:12Z")

</div>

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.

```python
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()
rad, radstep = 0, 2 * pi / steps

for i in range(steps+1):
    xold, yold = x, y
    cr, sr = cos(rad), sin(rad)
    x = int(x0 + r*cr + (.5 if cr >= 0 else -.5))
    y = int(y0 + r*sr + (.5 if sr >= 0 else -.5))
    can.move(wid, x-xold, y-yold)
    rad += radstep
    can.update()
    sleep(.05)

```

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.
