简体   繁体   中英

How to make a program like mouse locator with python? (like Free Utility to Locate Mouse Cursor Position)

How to make a program like mouse locator with python? (like Free Utility to Locate Mouse Cursor Position) I don't know how to display the coordinates in the window when the mouse moves

import tkinter as tk
import pyautogui as pag

win = tk.Tk()
win.geometry('200x50+550+250')
win.title('Mos Loc')
win.config(bg='#323232')
win.attributes('-topmos',1)

lab = tk.Label(text ='')
lab.pack()

def pos():
   x = pag.position()
   lab.config(text = x)

You would need to update the text every x milliseconds to get an up-to-date coordinate of your cursor. The follow code does this using the frame.mainloop() function to keep the window alive, and the frame.after(MILLISECONDS, FUNCITON) to update the text.

import tkinter as tk
import pyautogui as pag

win = tk.Tk()
win.geometry('200x50+550+250')
win.title('Mos Loc')
win.config(bg='#323232')
win.attributes('-topmos', 1)

frame = tk.Frame()


lab = tk.Label(text='')
lab.pack()


def update():
    lab.configure(text=pag.position())
    frame.after(20, update)


update()
frame.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM