Python Forum
[Tkinter] How to update widgets?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to update widgets?
#1
Script,
from tkinter import *

main = Tk()
main.title("1. Unit & Dimentions")
#root.geometry("1600x800+0+0")

#Var Details
length = ["meter", "m", "fundamentals"]
mass = ["kilogram", "kg", "fundamentals"]
time = ["second", "s", "fundamentals"]
current = ["ampere", "A", "fundamentals"]
temperature = ["kelvin", "K", "fundamentals"]
amount_of_substance = ["mole", "mol", "fundamentals"]
luminous_intensity = ["candela", "cd", "fundamentals"]

Fundamentals = [length, mass, time, current, temperature, amount_of_substance, luminous_intensity]

#Text
name = Label(main, text = "1. Unit & Dimentions", font = ("arial", 45, "bold"), fg = "Steel Blue", bd = 10)
mj = Label(main, text = "A product of MihiraJ.com", font = ("arial", 26, "bold"), fg = "#cc5c54", bd = 10)
dev = Label(main, text = "Devloped by Oshadha Mihiranga", font = ("arial", 18, "bold"), fg = "Steel Blue", bd = 10)
search = Label(main, text = "Search", font = ("arial", 10, "bold"), fg = "black", bd = 10)
result_lbl = Label()

#Text boxes
search_box = Entry(main, borderwidth = 4, width = 34)

#Frames
results = LabelFrame(main, text = "Results")
rel_results = LabelFrame(main, text = "Related Results")

#errors
err001 = Label(results, text = "Err001 : Result not Found, Please type again.", fg = "#eb0000")

#functions
def no_input():
    err001.pack(padx = 6, pady = 6, side = LEFT)
    search_box.delete(0, END)

def result_found(name, result):
        search_box.delete(0, END)
        result_lbl = Label(results, text = "Physical quantity :  " + name + "\nUnit :  " + result[0] + "\nSymbol :  " + result[1], justify = LEFT).pack()

def search_start(txt_2_search):
    if txt_2_search == "":
        no_input()
    if txt_2_search == "mass":
        result_found("Mass", mass)

#buttons
search_button = Button(main, text = "Start Search", command = lambda : search_start(search_box.get().lower()))

#pack
name.pack()
mj.pack()
search.pack()
search_box.pack()
search_button.pack()
results.pack(fill = X)
result_lbl.pack(padx = 6, pady = 6, side = LEFT)
dev.pack()

mainloop()
What i want to do is,
When i search mass
it prints out details.
when i search mass again!
it updates the older widget, instead of spamming th gui
Reply
#2
I already gave you an example that does exactly what you are asking and the example was based on your code.

https://python-forum.io/Thread-Tkinter-H...#pid134606

This code in particular:
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)
In particular lines 5 or 7.

In addition to this method you could use "result_text.configure(text='whatever text goes in label')

You could also create a tkinter StringVar() and assign this as the textvariable when creating the label.
result_text = StringVar()
Label(results, textvariable = result_text, width= 40, height=3).pack()
..
def search(event):
    name = search_box.get()
    unit = units.get(name)
    if unit:
        result_text.set(f'Physical quantity :  {name}\nUnit :  {unit[0]}\nSymbol :  {unit[1]}')
    else:
        result_text.set(f'Unit "{name}" not found')
    search_box.delete(0, END)
Oshadha likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020