'how to update a tkinter label [duplicate]

enter image description herei wrote a programm, that gets weather data from a api, and now im working on a interface, but when i want a new temperature in the same label it doesnt work.

import requests
from tkinter import *

global stadt

TOKEN = "rrrrrrrrrr"
a = "jaaa"

root = Tk()

root.title("Wetter")
root.geometry("300x150")


def get_entry():
    stadt = Entry1.get()
    url = f"https://api.openweathermap.org/data/2.5/weather?q={stadt}&appid={TOKEN}"
    f = requests.get(url).json()

    temperatur = f["main"]["temp"]
    temperatur = temperatur - 273.15
    luftdruck = f["main"]["pressure"]

    alltext= f"Die Temperatur in {stadt} beträgt {round(temperatur)}°C\nDer Luftdruck in {stadt} beträgt {luftdruck}Ba"

    string = StringVar()
    lab = Label(root, text=alltext)
    lab.pack()
    string.set(alltext)
    

    

Label1 = Label(root, text="Bitte Stadt eingeben")
Label1.pack()

Entry1 = Entry(root)
Entry1.pack()



Button1 = Button(root, text="Senden", command=get_entry)
Button1.pack()


root.mainloop()

"""temperatur = u[2][2]
luftdruck = u[2][4]

temperatur = temperatur -273.15



print("Temperatur = ",round(temperatur),"°C")
print("Luftdruck = ",round(luftdruck),"Pa")


my goal is to update a label, without creating the same label again

on the picutre you can see the problem, i want it so that the text overwrites other one



Solution 1:[1]

Replace

    lab = Label(root, text=alltext)
    lab.pack()

with

    Label1['text'] = alltext

and I wouldn't bother with the StringVar But if you want to use StringVar that needs to be defined in the main code before Label1 is made, eg tvar = StringVar. The inital text in Label1 in this code would be assigned to tvar``. And then inside Label1dotextvariable = tvar. Then inside get_entrydotvar.set(alltext). Also remove lab``` label.

Best not use string as a variable name to avoid possible keyword conflict with Python.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1