在 Python 中使用 tkinter 库时,你可以使用 bind() 方法将事件绑定到某个组件上。在绑定事件时,你可以传入一些属性来定义事件的行为。
例如,你可以使用 bind() 方法将鼠标单击事件绑定到按钮上,并在事件函数中访问鼠标单击事件的 x 和 y 坐标:
from tkinter import *
def click(event):
print("clicked at", event.x, event.y)
root = Tk()
button = Button(root, text="Click Me")
button.bind("<Button-1>", click)
button.pack()
root.mainloop()