简体   繁体   中英

Cursor Tracking Python Tkinter

I am creating a chess and the first problem I have is implementing a creating the code that will allow me to get the current position of the mouse and print that image of the mouse in that mouse coordinates and basically loop it until the user says so. Right now it is just on a timer. Feel free to replace the image with another gif. This is only a partion of the code

I don't know what else to try. I've basically rearranged the code hoping that that was the problem but it wasn't.

def Mousecoords():

    pointxy = root.winfo_pointerxy()


    print(str(pointxy))

    canvas.create_image(pointxy[0], pointxy[1], image=whiteKing.pieceImage)
    time.sleep(0.3)


ok = 0


while ok <= 10:
    root.after(500, Mousecoords)
    ok += 1

Right now, what the code does is it gets the actual coordinates of the cursor in real time and after it has done so throughout the duration of the timer, it displays the images however, what I am trying to achieve is get the coordinates and the images getting printed out simulatenously.

right now, what it does is:

gets coordinates (1) gets coordinates (2) gets coordinates (3) gets coordinates (4) gets coordinates (5) gets coordinates (6) gets coordinates (7) gets coordinates (8) gets coordinates (9) gets coordinates (10)

prints image (1-10 at the same time)

instead what i want it to do is:

gets coordinates (1) displays image at (1) gets coordinates (2) displays image at (2) gets coordinates (3) displays image at (3) gets coordinates (4) displays image at (4) gets coordinates (5) displays image at (5) gets coordinates (6) displays image at (6) gets coordinates (7) displays image at (7) gets coordinates (8) displays image at (8) gets coordinates (9) displays image at (9) gets coordinates (10) displays image at (10)

if theres any other way or method of having an image follow the cursor, please enlighten me thank you Also please tell me if i didn't explain clearly or missed out a few bits

You should not use winfo_pointerxy() as it returns mouse position relative to screen origin (the top-left corner of screen). Use bind('<Motion>', callback) on canvas to track the mouse position relative to canvas (then you don't need to use .after() ). Also, you should not recreate the image every time you want to update the image, you should update the coordinate of the image instead.

Below is a sample code:

from tkinter import *
from PIL import Image, ImageTk

def Mousecoords(event):
    pointxy = (event.x, event.y) # get the mouse position from event
    print(pointxy)
    canvas.coords(cimg, pointxy) # move the image to mouse postion

root = Tk()
img = ImageTk.PhotoImage(file='_red.png')
canvas = Canvas(width=400, height=200)
cimg = canvas.create_image(200, 100, image=img)
canvas.pack()
canvas.bind('<Motion>', Mousecoords) # track mouse movement
root.mainloop()

And the output:

在此处输入图片说明

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