Bind/event 给出错误 'int' 对象没有属性 'bind'
Bind/event gives error 'int' object has no attribute 'bind'
我正在尝试了解绑定和事件在 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()
itemconfigure
会改变颜色:
def main():
root = tkinter.Tk()
title = tkinter.Label(root, text="Test Window")
title.pack()
canvas = tkinter.Canvas(root, background='green', width=300, height=300)
s1 = canvas.create_rectangle(0, 0, 100, 100, fill='magenta')
s2 = canvas.create_rectangle(100, 0, 200, 100, fill='blue')
s3 = canvas.create_rectangle(200, 0, 300, 100, fill='blue')
canvas.pack()
def change_square(event):
canvas.itemconfigure(s1, fill="blue")
canvas.bind("<Button-1>", change_square)
root.mainloop()
如果你想把中间变成黑色,你可以使用:
canvas.itemconfigure(s2, fill="black")`
以此类推
如果您想根据点击的颜色更改颜色,这应该可行:
def change_square(event):
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
sq = canvas.find_closest(x,y)[0]
canvas.itemconfigure(sq, fill="black")
canvas.bind("<Button-1>", change_square)
root.mainloop()
我正在尝试了解绑定和事件在 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()
itemconfigure
会改变颜色:
def main():
root = tkinter.Tk()
title = tkinter.Label(root, text="Test Window")
title.pack()
canvas = tkinter.Canvas(root, background='green', width=300, height=300)
s1 = canvas.create_rectangle(0, 0, 100, 100, fill='magenta')
s2 = canvas.create_rectangle(100, 0, 200, 100, fill='blue')
s3 = canvas.create_rectangle(200, 0, 300, 100, fill='blue')
canvas.pack()
def change_square(event):
canvas.itemconfigure(s1, fill="blue")
canvas.bind("<Button-1>", change_square)
root.mainloop()
如果你想把中间变成黑色,你可以使用:
canvas.itemconfigure(s2, fill="black")`
以此类推
如果您想根据点击的颜色更改颜色,这应该可行:
def change_square(event):
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
sq = canvas.find_closest(x,y)[0]
canvas.itemconfigure(sq, fill="black")
canvas.bind("<Button-1>", change_square)
root.mainloop()