How do i differentiate between two widgets on the same event

Question:

I want to have some input boxes which contain an text for the user to know what is required to enter. This text should disappear when the user clicks on it. How do i know which box the user clicked?

class window():
    def handleEvent(self,event):
        self.text.set("")
    def handleEvent2(self,event):
        a = self.efeld.get()
        print(a)
    def page0(self):
        self.text = tk.StringVar(None)
        self.text.set("Enter text here")
        self.efeld = ttk.Entry(fenster, textvariable=self.text)
        self.efeld.place(x=5, y=20)
        self.efeld.bind("<Button-1>",self.handleEvent)
        self.efeld.bind("<Return>",self.handleEvent2)
        self.text2 = tk.StringVar(None)
        self.text2.set("Enter text 2 here")
        self.efeld2 = ttk.Entry(fenster, textvariable=self.text2)
        self.efeld2.place(x=5, y=50)
        self.efeld2.bind("<Button-1>",self.handleEvent)
        self.efeld2.bind("<Return>",self.handleEvent2)
fenster = tk.Tk()
fenster.title("Test")
fenster.geometry("500x350")
fenster.resizable(False,False)
window().page0()
fenster.mainloop()

Answers:

You can use the event.widget attribute to get a reference to the widget that triggered the event.

Answered By: JRiggles

You can use the widget attribute of the event object. It is a reference to the widget that got the event.

def handleEvent2(self,event):
    a = event.widget.get()
    print(a)
Answered By: Bryan Oakley

Since you are using tkinker,
event.widget will contain what you want.

Sorry for short reply, navigating from mobile.

Answered By: Vadim Sayfi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.