My First Python Project – Building an Auto Clicker

My job requires me to stay signed into several virtual machines at once. As I am only human these eventually time out and I have to go re-connect back up to them, wasting valuable time and testing valuable patience.

Talking of saving time and patience I have included the whole code right at the bottom. If you don’t care how it works pop straight down and grab it.

I have been passively looking into Python I thought this would be the perfect opportunity to get my hands dirty and build something that is A.) useful and B.) doesn’t require me to follow a tutorial.

Import The Modules

Lets start with the modules I imported mouse and tkinter to perform the required functions. If you need these use pip to install them.

Mouse is what allows python to control the mouse and tkinter will be used for making a simple (And I do mean simple) GUI.

#Imports the modules I will use - If you are missing a module use PIP to install them.
import mouse
import time
import tkinter
from tkinter import *
Make The GUI

With the modules imported I moved onto making the window. This is done through tkinter which itself has hours and hours of tutorials for how to make the most of it.

These 3 lines call a Window, name it Clicker and set the size to 50×50 pixels which is very small.

#Initial parameters for the window
window = Tk()
window.title("Clicker")
window.geometry("50x50")
Write The Clicker Function

Now we need to create the function that will be called by the button. This is by far the longest part of the code and I ended up using a PowerShell script to get the correct cursor positions for the mouse because the target machine didn’t have Python.

PowerShell Script Source

#Powershell script to give mouse cursor position.
Add-Type -AssemblyName System.Windows.Forms
 
$X = [System.Windows.Forms.Cursor]::Position.X
$Y = [System.Windows.Forms.Cursor]::Position.Y
 
Write-Output "X: $X | Y: $Y"

The output from this PowerShell script gave me the values -2833,476 & -1327,561. Those are my target pixels so I am now ready to define the clicker function.

#Defines the clicker(): function called by the Button.
def clicker():
    mouse.move(-2833,476, absolute=True, duration=0.0) #Move cursor here
    mouse.click('left')
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    mouse.move(-1327,561, absolute=True, duration=0.0) #Move cursor here
    mouse.click('left')
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    window.after(600000, clicker) #Time in Milliseconds - 10 minute loop.

As you can see the commands are fairly logical. the cursor moves to the pixel, the mouse left clicks and then wiggles 10 pixels one way and 10 pixels the other way. After completing this first wiggle it moves to the next pixel and does it again. There is then a “Wait” for 10 minutes, this will allow you to terminate the programme properly. Without the “Window.after” command the script doesnt close and any attempt to close it causes a crash.

Create the Buttons

So we have our window, we have our function, now we just need 2 buttons, one to start the clicker and one to close the window and stop the clicker.

button = Button(window, text="Start Clicker", command=clicker) #Button calls the clicker(): function 
button.pack()

close_button = Button(window, text="Close", command=window.destroy) #Button closes the window
close_button.pack()

This code creates the buttons, you can change their size, colour, add padding etc but I have gone for plain old buttons.

The Whole Code

So when we put the whole code together it should look like:–

#Created to Click 2 seperate points and wiggle the mouse.

#Imports the modules I will use - If you are missing a module use PIP to install them.
import mouse
import time
import tkinter
from tkinter import *


#Initial parameters for the window
window = Tk()
window.title("Clicker")
window.geometry("50x50")

#Defines the clicker(): function called by the Button.
def clicker():
    mouse.move(-2833,476, absolute=True, duration=0.0) #Move cursor here
    mouse.click('left')
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    mouse.move(-1327,561, absolute=True, duration=0.0) #Move cursor here
    mouse.click('left')
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    mouse.move(-10,10, absolute=False, duration=0.1)
    mouse.move(10,-10, absolute=False, duration=0.1)
    window.after(600000, clicker)

	
button = Button(window, text="Start Clicker", command=clicker) #Button calls the clicker(): function 
button.pack()

close_button = Button(window, text="Close", command=window.destroy) #Button closes the window
close_button.pack()

window.mainloop()

I saved this file as a .py file and then used pyinstaller to extract the py file as a .exe which allows me to run it on any machine regardless of if python is installed or not. If you don’t know about Pyinstaller I have included a link to the instructions I followed:– Instructions for pyinstaller

like I said it is super simple but it does what I need it to do.

After using it for 2 days there are already some improvements I want to try and add, firstly I want to make it so the script returns the mouse to its initial location before the function runs. This will make it less invasive for the user as they wont constantly have to be retrieving the cursor.

Secondly I like the idea of a “Set” function, to allow users to select various parts on their screen real estate and have the script note the pixels down into a list to iterate through once the start clicker button is triggered.

These are some way off yet but I think once they’re included this little clicker wont be half bad.

Leave a Reply

Your email address will not be published.