Reputation: 5
newWindow = Toplevel(app)
newWindow.geometry("400x400+580+190")
Label(newWindow, text="choose the food").pack()
choice = Listbox(newWindow)
choice.pack()
food_f = open("food.txt")
for line in food_f:
f = {}
(f['food'], f['unit'], f['kcal'], f['standard']) = line.split(";")
choice.insert(END, f['food'])
t = StringVar()
t.set(choice.curselection())
Label(newWindow,textvariable=t).pack()
I want the label to print out in real time what is selected in the list box. But above code can't.
Upvotes: 0
Views: 63
Reputation: 385900
You can create a binding on the virtual event <<ListboxSelect>>
, and from within the bound function you can get the selected item and update the label using the configure
method.
For example, given a listbox named listbox
and a label named label
, you can update the label like this when the selection changes:
def update_label(event):
curselection = listbox.curselection()
if curselection:
data = listbox.get(curselection[0])
label.configure(text=data)
else:
label.configure(text="")
listbox.bind("<<ListboxSelect>>", update_label)
Upvotes: 1