'Interference of canvas items and problem in setting coordinates

I'm working on an animation of a moving object, while drawing it's path. I want to draw the pixels in which the center of the object went through... but guess what? python decided to set the NW anchor of the image with the coordinates I send, instead of the center. I infer it has something to do with the pixels I draw simultaneously (creating a one pixel rectangle). so the image appear on the right of the path bellow... I want the center of it to be on the top of the pixels... adding the main of the code:

from tkinter import*
import time

dt = 0.01
clock_place = (500, 10)


def round_two(t, t0):
    return round((t-t0)*100)/100


def round_three(t, t0):
    return round((t-t0)*1000)/1000


# showing 'real time motion' for a known path (also cyclic), with
# parametric representation

def paint_known_path(x_pos, y_pos, t_0):
    window = Tk()
    canvas = Canvas(window, height=700, width=1000)
    canvas.pack()
    canvas.config(background='black')
    tennis_ball = PhotoImage(file='tennis ball.png')

    t = t_0
    x = x_pos(t_0)
    y = y_pos(t_0)
    particle = canvas.create_image(x, y, image=tennis_ball)
    clock = canvas.create_text(clock_place, text=round_two(t, t_0), 
    fill='white')
    while True:
        canvas.create_rectangle(x, y, x, y, outline='red')
        canvas.itemconfig(clock, text=round_two(t, t_0))
        t += dt
        x = x_pos(t)
        y = y_pos(t)
        canvas.moveto(particle, x, y)
        window.update()
        if x == x_pos(t_0) and y == y_pos(t_0):
            if t - t_0 > 100*dt:
                break
        time.sleep(dt)

    canvas.create_text((500, 100), text='orbit duration: ' + 
    str(round_three(t, t_0)), fill='white')
    window.mainloop()


Solution 1:[1]

It turns out to be quite a bit require, but here is the main completion components.

The first additional part that you need to add:

# print('the ten ball height', tennis_ball.height(), tennis_ball.width())
# tennis ball dimensions
tb_hght = tennis_ball.height()
tb_wdth = tennis_ball.width()
mid_point_x = x + tennis_ball.height() / 2
mid_point_y = y + tennis_ball.width() / 2

Secondly, also needed to add some functions to for x_pos and y_pos like this (these are just example functions to make the code work):

def x_pos(a):
    # any function of t,
    return 100

def y_pos(a):
    # any function of t,
    return 100

Furthermore, you need to call the function at the end like this:

paint_known_path(x_pos,y_pos,0)

Finally, need to add the mid_point_x and mid_point_y to the path that is drawn (as these will be the image centre points).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D.L