ItayMiz
ItayMiz

Reputation: 789

I'm trying to show to mouse position on a window, but I can't update the text on the window

The text on the window stays the same as it was in the start.

from pynput.mouse import Controller


from tkinter import *


root = Tk()

mouse = Controller()

Label(root, text=mouse.position).pack()

root.mainloop()

Upvotes: 0

Views: 187

Answers (1)

TywinLannister88
TywinLannister88

Reputation: 92

You need to use Listener for pynput:

from pynput.mouse import Controller
from pynput.mouse import Listener
from tkinter import *
from time import sleep

root = Tk() 
mouse = Controller()

var = StringVar()
var.set(str(mouse.position))

def on_move(x, y):
    var.set(str((x,y)))

ll = Label(root, textvariable = var)

ll.pack()
with Listener(on_move=on_move) as listener:
    root.mainloop()

This worked for me.

Upvotes: 2

Related Questions