Home > Net >  How do i differentiate between two widgets on the same event
How do i differentiate between two widgets on the same event

Time:11-30

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

CodePudding user response:

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)

CodePudding user response:

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

  • Related