Skip to main content


Get Mouse Position in Real Time using Python | Mouse Position Application

While using pyautogui module I always come across the same problem that is getting the X & Y mouse coordinates for pyautogui.moveTo(X,Y) which is not accurate or correct.


I seem to struggle getting the correct mouse position every time I use pyautogui module


Well as the title suggest what this post is about so ya I made myself a simple application in python which will give me coordinates in real time and I can re-use this applicaiton again and again and again with ease.


This is the final result. Hope it helps some one out there.

Get Mouse Position in Real Time using Python

Mouse Position Application Code

# Import the required libraries
from tkinter import *
import pyautogui
    
root=Tk()
root.title("Mouse Position Application in Real Time")

# Getting Your Screen Size
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
print(ws,hs)

def mouse_position(event):
   x, y = pyautogui.position()
   m = ("X: "+str(x)+'\n'+ "Y: "+str(y))
   x1=event.x
   y1=event.y
   x2=event.x
   y2=event.y
   # Draw an oval in the given co-ordinates
   canvas.create_oval(x1,y1,x2,y2,fill="black", width=5)

   # In simple format
   #canvas.create_text(x2+0,y2+15,text=f"{m}")

   # Same as above but with font, size, color
   # Anchor East = e, west = w, north = n, south = s
   canvas.create_text(x2+0,y2-15,text=f"{m}", anchor='e', font=("Courier", 10), fill='red')

# Create a canvas widget
canvas = Canvas(root, width=f"{ws}", height=f"{hs}", bg = '#F5FFFA', highlightthickness = 0)
canvas.pack()

# To Make Full Screen
#canvas.master.overrideredirect(True)

# Make screen transparent using alpha
canvas.master.wm_attributes("-alpha",0.5)

# Keep it above all other window screens
canvas.master.wm_attributes("-topmost",True)

# Just for fun
#canvas.bind('<Motion>', mouse_position)

# To show result after release use <ButtonRelease-1>
canvas.bind('<ButtonPress>', mouse_position)

mainloop()

:::Dev Info:::

My Name:- Joel D'costa
Git:- mouse_position_application_RealTime

Comments

  1. Very Informative and creative contents. Keep posting More Blogs

    ReplyDelete
  2. https://xiipython.blogspot.com/2020/02/our-great-achievement-in-47th-jnnsmme.html#comment-form

    ReplyDelete

Post a Comment



🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)

Top 11 Essential Python Tips and Tricks





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram