Python事件绑定标签,事件和绑定在不同类中

Python事件绑定标签,事件和绑定在不同类中

问题描述:

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

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") 

当我开始它,所以我得到这个错误:

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' 

如果有人能帮助我,我gratefull ^^

编辑1: 下面是完整的代码

#Mein Erstes Gui Python Programm mit Tkinter 
#Created: July,2017 
#Creator: Yuto 
from tkinter import * 

#class für den Inhalt des Windows z.b. label 
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) 
     label.place(x=300, y=50, width="200", height="20") 


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


#class für das Window an sich hier wird dann auch z.b. Inhalt eingebunden 
class Window(WindowInhalt): 
    def __init__(self): 
     super().__init__() 
     self.tkWindow = Tk() 
     self.label() 
     self.windowSettings() 

    #settings für das window z.b. größe 
    def windowSettings(self): 
     self.tkWindow.configure(background="lightyellow") 
     self.tkWindow.title("GUI LALALLALALA") 
     self.tkWindow.wm_geometry("800x400+600+300") 
     self.tkWindow.mainloop() 


#Only ausführen wenn es nicht eingebunden ist 
if __name__ == "__main__": 
    print("starten") 
    w = Window() 
else: 
    print("Dise Datei bitte nicht einbinden!") 
+1

你是如何初始化一个实例的?你只是显示错误。 – JClarke

在你的代码需要创建的EventsBinding一个实例,然后调用在该实例方法

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

如果你不想创建一个实例,你需要定义你的方法为静态方法

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

啊好吧我不知道,谢谢 – Yuto

+0

@Yuto:这不是Tkinter特有的东西。这就是python类的工作原理。 –

+0

是的,我想起它,而且我知道它。但在这一刻,我忘记了我必须这样做。 – Yuto