Insert and delete on focus of loop widget

how can i do this? this works but only with the last ScrolledText, i need it to work with the specific ScrolledText, been playing with name but cant seem to get it to work.

Any ideas?

from tkinter.scrolledtext import ScrolledText
from tkinter import Tk

root = Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))




for x in range(6):
    kiblogbn = ScrolledText(root, name="frm_"+x, width=25, height=30, border=2, relief="solid")
    kiblogbn.grid(column=x, row=0, padx=10, pady=10, ipady=25, sticky="W")

    kiblogbn.insert("1.0", 'Copy logs here.')
    kiblogbn.bind("<FocusIn>", lambda _: kiblogbn.delete('1.0', 'end'))
    kiblogbn.bind("<FocusOut>", lambda _: kiblogbn.insert("1.0", "Copy logs here." if str(kiblogbn.get("1.0", "end")) == "n" else ""))




if __name__ == "__main__":
    root.mainloop()

Answer

Change the two lines:

kiblogbn.bind("<FocusIn>", lambda _: kiblogbn.delete('1.0', 'end'))
kiblogbn.bind("<FocusOut>", lambda _: kiblogbn.insert("1.0", "Copy logs here." if str(kiblogbn.get("1.0", "end")) == "n" else ""))

to

kiblogbn.bind("<FocusIn>", lambda e: e.widget.delete('1.0', 'end'))
kiblogbn.bind("<FocusOut>", lambda e: e.widget.insert("1.0", "Copy logs here." if str(e.widget.get("1.0", "end")) == "n" else ""))

Also name="frm_"+x will raise error because x is integer. Change it to name="frm_"+str(x).


Update: delete content when focus out only if the content is the same as the placeholder:

PLACEHOLDER = 'Copy logs here.'

for x in range(6):
    kiblogbn = ScrolledText(root, name="frm_"+str(x), width=25, height=30, border=2, relief="solid")
    kiblogbn.grid(column=x, row=0, padx=10, pady=10, ipady=25, sticky="W")

    kiblogbn.insert("1.0", PLACEHOLDER)
    kiblogbn.bind("<FocusIn>", lambda e: e.widget.delete("1.0", "end-1c") if e.widget.get("1.0", "end-1c") == PLACEHOLDER else None)
    kiblogbn.bind("<FocusOut>", lambda e: e.widget.insert("1.0", PLACEHOLDER if e.widget.get("1.0", "end-1c") == "" else ""))

Recommend to use functions instead of lambdas for better readability.