-1

What I am trying to do is make something that tracks the mouse's position.

from pyautogui import position #using as mouse tracker 
from tkinter import * 
from playsound import playsound #using this later 
from time import sleep #delay

root = Tk()
root.geometry("1000x500") 
def loop():
    label = Label(root, text=str(position()))
    label.pack()
    sleep(0.01)
loop()


root.mainloop()

When I run it it displays the position that the mouse was first in. If I run:

from pyautogui import *
from tkinter import *
from playsound import playsound
from time import sleep
root = Tk()
root.geometry("1000x500")
a = 1
while a == 1:
     label = Label(root, text=str(position())
     label.pack()
     time.sleep(0.01)
     


root.mainloop()

It runs a blank tkinter window. How can I make it actively change to match where the position is?

  • sleep and guis dont play well together. There is a method called `after` which may do what you want or else look at the suggested dupe [here](https://stackoverflow.com/questions/22925599/mouse-position-python-tkinter). I didnt want to mark it as a dupe myself as it might not be and it will close the question. – Paul Rooney Mar 02 '21 at 01:59
  • 1
    Use `root.after(100,loop)`. – Delrius Euphoria Mar 02 '21 at 03:59

1 Answers1

0

This question has been answered.

Mouse Position Python Tkinter

You can bind the mouse movement to a function and then edit your label from that function.

yes
  • 33
  • 5