Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter中单击类中的事件_Python_Tkinter_Click - Fatal编程技术网

Python 在tkinter中单击类中的事件

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

我正在尝试创建一个单击事件,以便轻松查看网格上的坐标。我一直在学习effbot教程,但它在我的课堂上似乎不起作用。以下是我所拥有的:

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)