I want to create a PanedWindow with variable number of panes including a child label bound to a function that prints the name of that label to the screen. My code is:
from tkinter import *
from tkinter import ttk
n = 6
root = Tk()
root.geometry('250x500+100+100')
p = ttk.Panedwindow(root, orient=VERTICAL)
p.grid(column=0, row=0, sticky=(N, S, W, E))
def write(message):
print(message)
for i in range(n):
message = 'This is label number %d' %i
pane = ttk.Labelframe(p, width=25)
p.add(pane)
label = ttk.Label(pane, width=10, text='Label %d' %i, relief='solid', cursor='hand2')
label.bind('<Button-1>', lambda e: write(message))
label.grid(padx=5, pady=5)
root.mainloop()
But no matter which label is clicked the output corresponds to the last label. How can I solve this problem?