简体   繁体   中英

(Tkinter) Why doesn't itemconfig change the colors of my objects?

I am trying to make a code that changes the color of a series of objects, which form a mesh, in the form of a gradient between two colors in a horizontal direction. This change will be made when another object called "ball" is added. I put the objects in an array, and defined a function that iterates this change with canvas.itemconfig , however the objects don't change color.

import tkinter
import numpy as np

#Sizes
animation_window_width= 12800
animation_window_height= 720
animation_ball_radius = 12.5
    
#Function to put the ball  
def place_ball(event): 
    canvas.unbind("<Motion>")
    plate_animation()
    
#Function to locate the ball 
def locate_ball(event):
    canvas.coords(ball,
        event.x-animation_ball_radius,
        event.y-animation_ball_radius,
        event.x+animation_ball_radius,
        event.y+animation_ball_radius)
    canvas.coords(tball,
        event.x, event.y)

#Function to add the ball
def add_ball():
    canvas.bind('<Motion>', locate_ball)
    canvas.bind('<Button-1>', place_ball)

#Color gradient function
def compute_colors(start, end, limit):
    (r1,g1,b1) = window.winfo_rgb(start)
    (r2,g2,b2) = window.winfo_rgb(end)
    r_ratio = float(r2-r1) / limit
    g_ratio = float(g2-g1) / limit
    b_ratio = float(b2-b1) / limit
    colors = []
    for i in range(limit):
        nr = int(r1 + (r_ratio * i))
        ng = int(g1 + (g_ratio * i))
        nb = int(b1 + (b_ratio * i))
        color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb)
        colors.append(color)

    return colors

#Function to animate the mesh
def plate_animation(): 
    color = compute_colors("red3", "blue", len(plate[0]))
    for o in range(len(plate)):
        for p in range(len(plate[0])):
            canvas.itemconfig(plate[o][p], fill=color[p])
    
    
window = tkinter.Tk()
window.title("Mesh simulation")
window.geometry(f'{animation_window_width}x{animation_window_height}')
canvas = tkinter.Canvas(window)
canvas.configure(bg="black")
canvas.pack(fill="both", expand=True)

plate=np.zeros([12, 17])

for i in range (0, 1250, 78):
    for j in range (0, 690, 62):
        plate[j//62, i//78]=canvas.create_oval(i+5,j+5,i+30,j+30,fill="grey", outline="white", width=2)
        
ball = canvas.create_oval(0,0,0,0,fill="red", outline="white", width=2)
tball = canvas.create_text(0,0, font=("Impact", 8, "bold"), text="Ball")
tkinter.Button(window, text="Add ball", command=add_ball).pack()
window.mainloop()

Any idea what I'm doing wrong? Taking advantage, I would also like a way to limit the position that the ball can take so that it can only take that of any of the objects in the mesh.

It is because you did not specify the dtype for plate and it will be float number, but canvas.itemconfig() expects an integer item ID.

Specify an integer dtype when you create plate to solve the issue:

plate=np.zeros([12, 17], dtype=np.uint32)

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