If I have a Tkinter Text widget with the text bound to a mouse click, how can I keep track of which text the user clicks on? I want the function to return the number of the label clicked, but as it stands it only prints the last value of n which is 5. For example, if the user clicks "Name 1", I want it to print "1", if the user clicks on "Name 2", I want it to return "2".
Here is the code:
import tkinter as tk def prt(num): print(num) root = tk.Tk() t = tk.Text(root, height=20, width=50) t.pack() for n in range(1, 6): t.insert(tk.END, "%s %d\n" % ("Name", n), "label") t.tag_bind("label", "<Button-1>", lambda event, num = n: prt(num)) root.mainloop()
Any help appreciated.