может ли кто-нибудь помочь мне с этим: у меня есть список словарей, в которых я храню информацию для будущих прямоугольников Tkinter. После создания прямоугольников я пытаюсь заставить их менять цвет в зависимости от изменения значения «x», но это работает только для последнего созданного объекта, а не для остальных, вот код:
from tkinter import *
a = {
"y":2,
"len" : 4
}
b = {
"y":5,
"len" : 7
}
c = {
"y":6,
"len" : 8
}
d = {
"y":2,
"len" : 4
}
e = {
"y":4,
"len" : 12
}
f = {
"y":3,
"len" : 10
}
g = {
"y":7,
"len" : 10
}
groupe = [a,d, b, c, g, e, f]
xt1 = 800
xt2= 800
yt1=720
yt2=0
master = Tk()
root = Canvas(master, width=800, height=720)
root.pack()
#Moving RedLine
def deplacer():
global xt1, yt1, xt2, yt2
xt1-=5
xt2-=5
root.coords(laser_print, xt1, yt1, xt2,yt2)
root.after(50,deplacer)
return
#drawing blocks
graf_dist = 0
dist_betw=0
untouched = "blue"
being_touched = "red"
touched= "gray"
block_color=untouched
for ensemble in groupe:
graf_dist+=60
dist_betw+=10
xo=graf_dist+dist_betw
yo=int(ensemble['y']*50)
xl=xo+60
yl=int(ensemble['len']*50)
if xo<=xt1 and xl>xt1:
block_color=being_touched
elif xo<xt1 and xl<=xt1:
block_color=untouched
drawing_block = root.create_rectangle(xo, yo, xl, yl, fill=block_color, tag="blocks")
#draw horizontal redline
laser_print=root.create_line(xt1, yt1, xt2, yt2, fill="red")
#updating the color of the rectangles
def update_color():
x_block=xt1+1
untouched = "blue"
being_touched = "red"
touched= "gray"
block_color=untouched
if xo<=x_block and xl>x_block:
block_color=being_touched
elif xo<x_block and xl<=x_block:
block_color=untouched
root.itemconfig(drawing_block, fill=block_color)
root.after(50,update_color)
update_color()
deplacer()
mainloop()