Skip to content

Instantly share code, notes, and snippets.

@gostrafx
Last active April 3, 2025 02:13
Show Gist options
  • Save gostrafx/cea3b1a1f311e9cab7fc886484175914 to your computer and use it in GitHub Desktop.
Save gostrafx/cea3b1a1f311e9cab7fc886484175914 to your computer and use it in GitHub Desktop.
Tracking Mouse Pointer Borderless Window with Movement Controls
import os
import tkinter as tk
import pyautogui
from PIL import Image, ImageTk, ImageOps
from screeninfo import get_monitors
class BorderlessApp:
def __init__(self, root):
self.root = root
self.root.title("Tracking Mouse Pointer")
self.monitor = get_monitors()[0]
self.screen_width = self.monitor.width
self.screen_height = self.monitor.height
self.is_fullscreen = False
self.start_x, self.start_y = 0, 0
self.window_width, self.window_height = 800, 600
self.root.overrideredirect(True)
self.root.geometry(f"{self.window_width}x{self.window_height}+100+100")
self.setup_ui()
"""
a simple image represents desktop
https://i.imgur.com/73zXANQ.png
"""
self.load_image("your_image.png")
self.setup_bindings()
self.update_cursor()
def setup_ui(self):
self.canvas = tk.Canvas(self.root, bg='black')
self.canvas.pack(fill=tk.BOTH, expand=True)
self.cursor = self.canvas.create_oval(0, 0, 10, 10, fill="red", outline="white")
self.border = self.canvas.create_rectangle(1, 1, self.window_width - 1, self.window_height - 1, outline="#444444", width=2)
def load_image(self, image_path):
if os.path.exists(image_path):
self.img = Image.open(image_path)
self.img = ImageOps.expand(self.img, border=2, fill='#444444')
self.update_image_display()
else:
print(f"Image not found: {image_path}")
def update_image_display(self):
if hasattr(self, 'img'):
display_width = self.root.winfo_width()
display_height = self.root.winfo_height()
resized_img = self.img.resize((display_width, display_height), Image.LANCZOS)
self.photo = ImageTk.PhotoImage(resized_img)
if hasattr(self, 'image_id'):
self.canvas.delete(self.image_id)
self.image_id = self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)
self.canvas.coords(self.border, 1, 1, display_width - 1, display_height - 1)
self.canvas.tag_raise(self.cursor)
def start_move(self, event):
self.start_x = event.x
self.start_y = event.y
def do_move(self, event):
x = self.root.winfo_x() - self.start_x + event.x
y = self.root.winfo_y() - self.start_y + event.y
self.root.geometry(f"+{x}+{y}")
def toggle_fullscreen(self, event=None):
if not self.is_fullscreen:
self.window_width = self.root.winfo_width()
self.window_height = self.root.winfo_height()
self.root.geometry(f"{self.screen_width}x{self.screen_height}+0+0")
self.is_fullscreen = True
else:
self.root.geometry(f"{self.window_width}x{self.window_height}+100+100")
self.is_fullscreen = False
self.root.after(100, self.update_image_display)
def close_app(self, event=None):
self.root.destroy()
def update_cursor(self):
try:
screen_x, screen_y = pyautogui.position()
canvas_width = self.canvas.winfo_width()
canvas_height = self.canvas.winfo_height()
if canvas_width > 0 and canvas_height > 0:
image_x = int((screen_x / self.screen_width) * canvas_width)
image_y = int((screen_y / self.screen_height) * canvas_height)
self.canvas.coords(self.cursor, image_x - 5, image_y - 5, image_x + 5, image_y + 5)
self.root.after(20, self.update_cursor)
except:
pass
def setup_bindings(self):
self.canvas.bind("<Button-3>", self.start_move)
self.canvas.bind("<B3-Motion>", self.do_move)
self.canvas.bind("<Double-Button-1>", self.toggle_fullscreen)
self.root.bind("<Escape>", self.close_app)
self.root.bind("<Configure>", lambda e: self.update_image_display())
def safe_exit(self):
try:
self.root.destroy()
except:
pass
if __name__ == "__main__":
try:
root = tk.Tk()
app = BorderlessApp(root)
root.mainloop()
except Exception as e:
print(f"Fatal error: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment