Automated Mouse Mover Application with Tkinter

Introduction

In today’s fast-paced world, efficiency and productivity are crucial aspects of our daily lives. As a software developer, I strive to create tools that not only make life easier but also showcase the capabilities of programming. One such project that I’m excited to share is the “Mouse Mover” application developed using the Tkinter library in Python.

Overview

The Mouse Mover application is a small yet powerful utility designed to automatically move the mouse cursor in circular patterns on the screen. This can be particularly useful in scenarios where keeping the system active is necessary, such as preventing screen lock or showcasing a presentation.

Technologies Used

  • Python: The programming language used for the application’s logic.
  • Tkinter: A GUI toolkit for creating the user interface.
  • Threading: Utilized for running the mouse movement in the background.
  • PyAutoGUI: A Python module for programmatically controlling the mouse and keyboard.

Features

1. User-Friendly Interface

The application boasts a simple and intuitive graphical user interface (GUI) developed with Tkinter. Users can input the desired circle radius and movement speed through entry widgets.

2. Start/Stop Functionality

Users can start and stop the mouse movement at any time with the click of a button. Convenient keyboard shortcuts (F3 to start, F4 to stop) provide an additional level of accessibility.

3. Error Handling

The application gracefully handles invalid inputs, such as non-numeric values for the radius and speed, ensuring a smooth user experience.

How to Use

  1. Circle Radius: Enter the desired radius of the circular mouse movement.
  2. Movement Speed: Specify the speed at which the mouse should traverse the circle.
  3. Start Button: Click the “Start” button or use the keyboard shortcut (F3) to initiate the mouse movement.
  4. Stop Button: Press the “Stop” button or use the keyboard shortcut (F4) to halt the mouse movement.

Code Snippets

User Interface Creation

self.radius_label = tk.Label(master, text="Circle Radius:")
self.radius_entry = tk.Entry(master)

self.speed_label = tk.Label(master, text="Movement Speed:")
self.speed_entry = tk.Entry(master)

self.start_button = tk.Button(master, text="Start", command=self.start_mouse_mover)
self.stop_button = tk.Button(master, text="Stop", state=tk.DISABLED, command=self.stop_mouse_mover)

Mouse Movement Logic

def move_mouse_in_circles(self, radius, speed):
    while self.is_running:
        angle = time.time() * speed
        x = int(radius * math.cos(angle)) + pyautogui.size()[0] // 2
        y = int(radius * math.sin(angle)) + pyautogui.size()[1] // 2

        pyautogui.moveTo(x, y)
        time.sleep(0.01)

Conclusion

The Mouse Mover application serves as a testament to the power of Python and its versatile libraries. Whether for practical use or as a learning tool, this project demonstrates how programming can automate tasks and enhance user experience.

Feel free to explore, modify, and integrate this application into your projects. The complete source code is available for download and adaptation, showcasing the potential for innovation using Python and Tkinter.

Final Code

import tkinter as tk
import threading
import pyautogui
import time
import math
import keyboard

class MouseMoverApp:
    def __init__(self, master):
        self.master = master
        self.master.title("Mouse Mover")

        # Increase window size and center it
        window_width = 400
        window_height = 200
        screen_width = master.winfo_screenwidth()
        screen_height = master.winfo_screenheight()
        x_position = (screen_width - window_width) // 2
        y_position = (screen_height - window_height) // 2
        self.master.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")

        # Circle Radius
        self.radius_label = tk.Label(master, text="Circle Radius:")
        self.radius_label.pack(pady=5)

        self.radius_entry = tk.Entry(master)
        self.radius_entry.pack(pady=5)

        # Movement Speed
        self.speed_label = tk.Label(master, text="Movement Speed:")
        self.speed_label.pack(pady=5)

        self.speed_entry = tk.Entry(master)
        self.speed_entry.pack(pady=5)

        # Start and Stop Buttons
        self.start_button = tk.Button(master, text="Start", command=self.start_mouse_mover)
        self.start_button.pack(pady=10)

        self.stop_button = tk.Button(master, text="Stop", state=tk.DISABLED, command=self.stop_mouse_mover)
        self.stop_button.pack(pady=10)

        self.is_running = False
        self.mouse_mover_thread = None

        # Bind F3 to start and F4 to stop
        keyboard.on_press_key("F3", self.start_mouse_mover)
        keyboard.on_press_key("F4", self.stop_mouse_mover)

    def start_mouse_mover(self, event=None):
        if not self.is_running:
            try:
                radius = float(self.radius_entry.get())
                speed = float(self.speed_entry.get())
            except ValueError:
                tk.messagebox.showerror("Error", "Invalid input. Please enter valid numbers.")
                return

            self.is_running = True
            self.mouse_mover_thread = threading.Thread(target=self.move_mouse_in_circles, args=(radius, speed))
            self.mouse_mover_thread.start()

            self.start_button.config(state=tk.DISABLED)
            self.stop_button.config(state=tk.NORMAL)

    def stop_mouse_mover(self, event=None):
        if self.is_running:
            self.is_running = False
            self.mouse_mover_thread.join()

            self.start_button.config(state=tk.NORMAL)
            self.stop_button.config(state=tk.DISABLED)

    def move_mouse_in_circles(self, radius, speed):
        while self.is_running:
            angle = time.time() * speed
            x = int(radius * math.cos(angle)) + pyautogui.size()[0] // 2
            y = int(radius * math.sin(angle)) + pyautogui.size()[1] // 2

            pyautogui.moveTo(x, y)
            time.sleep(0.01)

if __name__ == "__main__":
    root = tk.Tk()
    app = MouseMoverApp(root)
    root.mainloop()

Acknowledgments

Special thanks to the open-source community for providing invaluable resources and tools that enable developers to create applications like the Mouse Mover. Let’s continue to collaborate and build upon each other’s successes in the world of software development.

One response to “Automated Mouse Mover Application with Tkinter”

  1. A WordPress Commenter Avatar

    Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

Leave a Reply

Your email address will not be published. Required fields are marked *

You might also like