Canvas 未绑定到 class 内的鼠标按钮,tkinter-Python3
Canvas not binding to mouse button within a class, tkinter-Python3
我一直在尝试将 canvas 绑定到鼠标点击,如 but within a class. The callback
function is not getting invoked though. All 中所述,此处似乎是在尝试绑定时调用 callback()
函数,而不是引用它。我正在引用它,但它仍然不起作用。
from tkinter import *
class BindingTrial():
def __init__(self,root,canvas):
self.root = root
self.canvas = canvas
self.canvas.bind("Button-1",self.callback)
def callback(self,event):
print ("clicked at", event.x, event.y)
root = Tk()
canvas= Canvas(root, width=100, height=100)
bt = BindingTrial(root,canvas)
canvas.pack()
root.mainloop()
您需要使用 "<Button-1>"
调用按钮绑定,并且回调应该接受 self 作为第一个参数。
class BindingTrial():
def __init__(self,root,canvas):
self.root = root
self.canvas = canvas
self.canvas.bind('<Button-1>',self.callback)
def callback(self, event):
print ("clicked at", event.x, event.y)
我一直在尝试将 canvas 绑定到鼠标点击,如 callback
function is not getting invoked though. All callback()
函数,而不是引用它。我正在引用它,但它仍然不起作用。
from tkinter import *
class BindingTrial():
def __init__(self,root,canvas):
self.root = root
self.canvas = canvas
self.canvas.bind("Button-1",self.callback)
def callback(self,event):
print ("clicked at", event.x, event.y)
root = Tk()
canvas= Canvas(root, width=100, height=100)
bt = BindingTrial(root,canvas)
canvas.pack()
root.mainloop()
您需要使用 "<Button-1>"
调用按钮绑定,并且回调应该接受 self 作为第一个参数。
class BindingTrial():
def __init__(self,root,canvas):
self.root = root
self.canvas = canvas
self.canvas.bind('<Button-1>',self.callback)
def callback(self, event):
print ("clicked at", event.x, event.y)