2016-11-12 7 views
2

私はこのグリッド上の任意の正方形を押して、私はそれが黄色にしたいです。しかし、itemconfigを使って最も最近のブロックを黄色にすることしかできません。私は何をしますか?いくつかの実験の後どのように私はキャンバスにPythonのオブジェクトを区別する

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

、私は、このソリューションに来た:

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() 
関連する問題