我想按下此网格上的任何方格,我希望它变为黄色。但是我只能使用itemconfig将最近的块创建为黄色。我该怎么办?
from tkinter import *
master = Tk()
canv = Canvas(master, width=200, height=100)
canv.pack()
def select(event):
print("Coordinates:", event.x, ",",event.y)
canv.itemconfig(grid,fill="yellow")
y=1
for i in range(1,6):
for j in range(0,10):
grid=canv.create_rectangle(1+(20*j),y,20+(20*j),20+y,fill="red")
canv.tag_bind(grid,'<Button-1>',select)
y+=20
mainloop()
答案 0 :(得分:0)
经过一些实验,我找到了这个解决方案:
from Tkinter import *
master = Tk()
canv = Canvas(master, width=200, height=100)
canv.pack()
def select(event, grid):
print("Coordinates:", event.x, ",",event.y)
canv.itemconfig(grid,fill="yellow")
y=1
for i in range(1,6):
for j in range(0,10):
grid=canv.create_rectangle(1+(20*j),y,20+(20*j),20+y,fill="red")
canv.tag_bind(grid, '<Button-1>', lambda event, var=grid: select(event, var))
y+=20
mainloop()