0

My Problem is that i have the binding from the label to his event and the event in different classes but i dont know really how to assign to the label the event in the class.I try it so:

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

And here is the event class:

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

When i start it so i get this error:

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'

If anybody can help me i am gratefull ^^

EDIT 1: Here is the full code

#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
  • 1
    How are you initializing an instance? You just show the error. Commented Jul 25, 2017 at 15:27

1 Answer 1

2

In your code you need to create an instance of EventsBinding, and then call the method on that instance

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

If you don't want to create an instance, you'll need to define your method as a static method

class EventsBinding(WindowInhalt):
    @staticmethod
    def Test(event):
        print("gedrückt")
Sign up to request clarification or add additional context in comments.

2 Comments

@Yuto: this isn't anything unique to Tkinter. This is simply how python classes work.
Yeah i think about it and than i know it.But on this moment i forgot that i have to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.