Python上的事件绑定,不同类中的事件和绑定

时间:2017-07-25 15:25:10

标签: python-3.x user-interface tkinter

我的问题是我从标签绑定到他的事件和不同类中的事件,但我真的不知道如何将标签分配给类中的事件。我试试这样:

let updateObject = {};
updateObject[`eventData/${eventId}`] = event;
updateObject[`userEvents/${uid}/${eventId}`] = true;
updateObject[`eventGuests/${eventId}/${uid}`]= {
  attendance: 2 // and some other data
};

这是事件类:

class WindowInhalt():
    def label(self):
        label = Label(self.tkWindow, text="What the fuck", fg="black",bg="lightyellow", font=('Arial', 14))
        label.bind("<Button-1>", EventsBinding.Test) #here is the assign
        label.place(x=300, y=50, width="200", height="20")

当我启动它时,我收到此错误:

class EventsBinding(WindowInhalt):
    def Test(self, event):
        print("gedrückt")

如果有人可以帮助我,我很感激^^

编辑1: 这是完整的代码

Traceback (most recent call last):
  File "D:\Programme\python\Lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: callback() missing 1 required positional argument: 'event'

1 个答案:

答案 0 :(得分:2)

在您的代码中,您需要创建EventsBinding的实例,然后在该实例上调用该方法

events_binding = EventsBinding(...)
...
label.bind("<Button-1>", events_binding.Test)

如果您不想创建实例,则需要将方法定义为静态方法

class EventsBinding(WindowInhalt):
    @staticmethod
    def Test(event):
        print("gedrückt")