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

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

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

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)

#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)

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

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)
dev.pack()

mainloop()
So basically, when I click search_button , it calls out the function search_start.
Now, and search start, calls out result_found or no_input
What I want to do, is delete result_lbl and then pack result_lbl again with new data.

I tried doing result_lbl.pack_forget()
but I can find where to put it!

If you don't understand this question, pls msg me.
And if I did something wrong, I'm really sorry. Im new to forums.
Reply
#2
you need result_lbl.destroy()
Reply
#3
(Jan-06-2021, 06:17 PM)Larz60+ Wrote: you need result_lbl.destroy()

But where do I put it?
I tried but,
Error:
NameError: name 'result_lbl' is not defined
Can I please know where to put it?
Reply
#4
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()
Oshadha likes this post
Reply
#5
(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)
Larz60+ likes this post
Reply
#6
(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()

Also, how would I need to script and have handle over it?
Reply
#7
f'Physical quantity : {name}\nUnit : {unit[0]}\nSymbol : {unit[1]}' constructs a string. just like your code "Physical quantity : " + name + "\nUnit : " + result[0] + "\nSymbol : " + result[1]. It is a bit more compact and I find it easier to read.

A "handle" is just the value returned when you create a widget. In "search_box = Entry(main, width = 34)" the "handle" is the variable "search_box" that references the Entry object returned by the Entry function call. In your code you created the label inside a function.
def result_found(name, result):
    result_lbl = Label(results, text = "Physical quantity :  " + name + "\nUnit :  " + result[0] + "\nSymbol :  " + result[1], justify = LEFT)
    result_lbl.pack(padx = 6, pady = 6, side = LEFT)
The variable "result_lbl" is local to the result_found() function. When the function exits the variable is disposed of. If you want to do things with the Label you have to keep a reference around. I did this by creating the Label in the module scope. Variables declared in the module scope are visible to all functions, classes, whatever in the same module (file). If you want to create the label in a function you will have to do something that makes that label visible to the module context.

I think the best solution is what I posted. Create the label once and use it over and over, only changing the text.
Reply
#8
(Jan-07-2021, 03:34 PM)deanhystad Wrote: f'Physical quantity : {name}\nUnit : {unit[0]}\nSymbol : {unit[1]}' constructs a string. just like your code "Physical quantity : " + name + "\nUnit : " + result[0] + "\nSymbol : " + result[1]. It is a bit more compact and I find it easier to read.

A "handle" is just the value returned when you create a widget. In "search_box = Entry(main, width = 34)" the "handle" is the variable "search_box" that references the Entry object returned by the Entry function call. In your code you created the label inside a function.
def result_found(name, result):
    result_lbl = Label(results, text = "Physical quantity :  " + name + "\nUnit :  " + result[0] + "\nSymbol :  " + result[1], justify = LEFT)
    result_lbl.pack(padx = 6, pady = 6, side = LEFT)
The variable "result_lbl" is local to the result_found() function. When the function exits the variable is disposed of. If you want to do things with the Label you have to keep a reference around. I did this by creating the Label in the module scope. Variables declared in the module scope are visible to all functions, classes, whatever in the same module (file). If you want to create the label in a function you will have to do something that makes that label visible to the module context.

I think the best solution is what I posted. Create the label once and use it over and over, only changing the text.


Ok,
I do like to use it by changing the text.
Can you help me with it?
If you can,
I want a demo script, which creates a label, and changes its text.
Smile
Reply
#9
I provded a working example in my initial post
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