标签上的 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:ProgrammepythonLibtkinter__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: callback() missing 1 required positional argument: 'event'

如果有人能帮助我,我将不胜感激^^

编辑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!")

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

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

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

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

最新更新