0

I'm able to bind a mouse action on a single entry widget, meaning, by default a value will be present in the entry box, if i click then the default will be removed. How would i do the same when loop is used over individual boxes. If i click on any entry box, the default value must be removed

from tkinter import *

root = Tk()

def hello(event):
    ent.delete(0, END)

for x in range(4):
   ent=Entry(root,fg="grey")
   ent.insert(0, "dd/mm/yy")
   ent.pack()

   ent.bind('<Button-1>',hello)

root.minsize(400, 400)
root.mainloop()
Nalini V
  • 3
  • 1
  • 4
  • So are you asking how to call the same callback on all entries when one of them is clicked upon? or are you asking how to bind the same command to multiple different entries – Joshua Nixon Mar 22 '19 at 11:08
  • same command to different entries over the loop – Nalini V Mar 22 '19 at 11:14
  • Check out [this solution](https://stackoverflow.com/a/47928390/3714930) and also [this one](https://stackoverflow.com/a/55291506/3714930). – fhdrsdg Mar 22 '19 at 11:14
  • i edited the code where am seeing the change only in the last box irrespective of where i click – Nalini V Mar 22 '19 at 11:30
  • That's because at the end of the loop, `ent` is the last entry widget. You could replace `ent.delete(0, END)` with `event.widget.delete(0, END)`, but I'd recommend you take a look at the two answers I linked, they make a class inherited from Entry with this functionality added, which makes this a lot easier. – fhdrsdg Mar 22 '19 at 11:35

1 Answers1

0

Your problem is that you're hardcoding ent in your callback. The event object tells you which widget received the event, so use that instead.

def hello(event):
    event.widget.delete(0, END)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685