Python 在tkinter中单击类中的事件
我正在尝试创建一个单击事件,以便轻松查看网格上的坐标。我一直在学习effbot教程,但它在我的课堂上似乎不起作用。以下是我所拥有的:Python 在tkinter中单击类中的事件,python,tkinter,click,Python,Tkinter,Click,我正在尝试创建一个单击事件,以便轻松查看网格上的坐标。我一直在学习effbot教程,但它在我的课堂上似乎不起作用。以下是我所拥有的: class Keyboard(Frame): def __init__(self, root, press): Frame.__init__(self, root) self.press = press self.createWidgets() self.bind("<Button-1
class Keyboard(Frame):
def __init__(self, root, press):
Frame.__init__(self, root)
self.press = press
self.createWidgets()
self.bind("<Button-1>", self.click)
def click(event):
print("clicked at", event.x, event.y)
单击
是类键盘
的一种方法。这意味着它将始终被传递一个隐式的第一个参数(通常称为self
),该参数是对类本身的引用
您需要像这样定义单击:
def click(self, event):
否则,event
将接收self
的参数,而应该用于event
的参数将被保留
这里有一个关于Python中的self
的参考:您正在定义类函数单击
,因此必须将第一个参数作为self
类对象传递
所以请改变它
class Keyboard(Frame):
def __init__(self, root, press):
Frame.__init__(self, root)
self.press = press
self.createWidgets()
self.bind("<Button-1>", self.click)
def click(self, event):
print("clicked at", event.x, event.y)
类键盘(框架):
定义初始化(自、根、按):
帧.\uuuu init\uuuuu(self,root)
self.press=press
self.createWidgets()
self.bind(“,self.click)
def单击(自身,事件):
打印(“单击”,事件x,事件y)
class Keyboard(Frame):
def __init__(self, root, press):
Frame.__init__(self, root)
self.press = press
self.createWidgets()
self.bind("<Button-1>", self.click)
def click(self, event):
print("clicked at", event.x, event.y)