python - Tkinter: Change Label when OptionMenu selection changes -
i having trouble updating label based on selection option menu. below working example of code:
import tkinter tk tkinter import * secs = ['subject', 'difficulty', 'percent', 'grade', 'pass'] master = tk() v = stringvar(master) v.set(secs[0]) def on_option_change(event): #lab2.destroy() lab2 = tk.label(master, text=v.get()) lab2.grid(row=2, column=1) w = optionmenu(master, v, *secs, command=on_option_change) w.grid(row=1, column=0) lab1 = tk.label(master, text='current value:') lab1.grid(row=2, column=0, sticky=e) lab2 = tk.label(master, text=secs[0]) lab2.grid(row=2, column=1, sticky=w) mainloop() the issue old label remains behind new 1 when new option selected. however, when add code destroy old label, i, understandably, error:
unboundlocalerror: local variable 'lab2' referenced before assignment any suggestions on easy fix appreciated. rather novice tkinter , gui creation in general. , have been searching answer several hours, little avail.
make lab2 variable global writing "global lab2" inside on_option_change function @ first line.
Comments
Post a Comment