我试图了解绑定和事件在 python 中是如何工作的。例如,我创建了 3 个图块,并希望能够更改其中一个图块的颜色,但无法理解或弄清楚我哪里出错了。我不断得到:
AttributeError: 'int' object has no attribute 'bind'.
以下是代码,提前致谢:
import tkinter
def main():
root = tkinter.Tk()
title = tkinter.Label(root, text="Test Window")
title.pack()
canvas= tkinter.Canvas(root, background='green', width = 300, height = 300)
tile1=canvas.create_rectangle(0, 0, 100, 100, fill = 'magenta')
tile2=canvas.create_rectangle(100,0, 200,100, fill = 'blue')
tile3=canvas.create_rectangle(200,0, 300,100, fill = 'blue')
canvas.pack()
def change_square(event):
event.configure(background = 'blue')
tile1.bind("<Button-1>", change_square(tile1))
root.mainloop()
if __name__ == '__main__':
main()