How can I create an event related to an Entry element in tkinter in pyhton?

1

You already link events to buttons so when you click on a button something happens. Now I need that when I click on an Entry element, I also link to an event. For example I have a date field in which by default I type "dd / mm / yyyy" and I want that when the user clicks in that field that text is deleted Thanks, best regards.

    
asked by Alfredo Lopez Rodes 30.04.2018 в 09:59
source

1 answer

0

You just have to use the bind method of the widget of shift, in this case an Entry. The event that refers to the left click is '<Button-1>' :

import tkinter as tk

class App(tk.Frame):
    def __init__(self, root=None):
        tk.Frame.__init__(self, root)
        self.text = tk.StringVar()
        self.text.set("Escribe algo aquí...")
        self.entry = tk.Entry(bg='orange', textvariable=self.text, relief=tk.SUNKEN, width=50)
        self.entry.grid(row=0, column=0)
        self.entry.bind("<Button-1>", self.clear_entry)

    def clear_entry(self, event):
        self.text.set("")


if __name__ == "__main__":
     root = tk.Tk()
     app = App(root)
     app.mainloop()

    
answered by 30.04.2018 / 14:46
source