Jan-07-2021, 07:30 AM
(Jan-07-2021, 06:12 AM)deanhystad Wrote: You cannot put the delete anywhere because you no longer have a handle to the label. But that's ok, because you really don't want to delete the label. You want to change the label text.
from tkinter import * units = { 'length' : ['meter', 'm'], 'mass' : ['kilogram', 'kg'], 'time' : ['second', 's'], 'current' : ['ampere', 'A'], 'temperature' : ['kelvin', 'K']} def search(event): name = search_box.get() unit = units.get(name) if unit: result_text['text'] = f'Physical quantity : {name}\nUnit : {unit[0]}\nSymbol : {unit[1]}' else: result_text['text'] = f'Unit "{name}" not found' search_box.delete(0, END) main = Tk() Label(main, text = '1. Unit & Dimensions').pack() Label(main, text = 'Search').pack() search_box = Entry(main, width = 34) search_box.pack() search_box.bind('<Return>', search) results = LabelFrame(main, text = 'Results') results.pack() result_text = Label(results, width= 40, height=3) result_text.pack() mainloop()
What does the
f
do,here,
if unit: result_text['text'] = f'Physical quantity : {name}\nUnit : {unit[0]}\nSymbol : {unit[1]}' else: result_text['text'] = f'Unit "{name}" not found' search_box.delete(0, END)