python - 如何在 tkinter 中更新文本标签?

标签 python tkinter

每次我单击获取天气的按钮(我使用 Json 从网站获取信息)时,之前的文本都会保留,新文本会打印在其下方,但我希望更新旧文本。我怎样才能做到这一点?请帮忙。

from tkinter import *
import requests
import json

root = Tk()
root.title("Weather")
root.geometry("600x400")

def RealWeather():

    try:
        api_request = requests.get("http://api.openweathermap.org/data/2.5/weather?appid=c2fc19de6fcd6869c4a9e167ee0f2eb7&q=" + city.get())
        api = json.loads(api_request.content)
        location = api['name'] + ", " + api['sys']['country']
        temp = int(api['main']['temp'] - 273.15)
        ws = api['wind']['speed']
        status = api['weather'][0]['main']

        Labelloc = Label(root, text="Location",background='#5182cf').grid(row=2,column=0)
        Label_loc = Label(root, text=location ,background='#5182cf').grid(row=2, column=1)

        Labeltemp = Label(root, text="Temperature",background='#5182cf').grid(row=3,column=0)
        Label_temp = Label(root, text=temp,background='#5182cf').grid(row=3,column=1)

        Labelws = Label(root, text="Wind Speed",background='#5182cf').grid(row=4,column=0)
        Label_ws = Label(root, text=ws,background='#5182cf').grid(row=4,column=1)

        Labelstatus = Label(root, text="Status",background='#5182cf').grid(row=5,column=0)
        Label_status = Label(root, text=status,background='#5182cf').grid(row=5,column=1)


    except Exception as e:
        api = "Error..."

    EmptyLabel = Label(root, text="", background='#5182cf').grid(row=1, column=0)


butn = Button(root, text="Search", command=RealWeather).grid(row=0, column=3)
Label1 = Label(root, text="Enter City: ").grid(row=0,column=0)
city = Entry(root)
city.grid(row=0, column=1, padx=20)

root.mainloop()

最佳答案

以下是如何使用我在评论中提到的通用小部件 config() 方法来解决您的问题:

from tkinter import *
import requests
import json

root = Tk()
root.title("Weather")
root.geometry("600x400")


def create_labels():
    global Label_loc, Label_temp, Label_ws, Label_status

    Label(root, text="Location",background='#5182cf').grid(row=2,column=0)
    Label_loc = Label(root, background='#5182cf')
    Label_loc.grid(row=2, column=1)

    Label(root, text="Temperature",background='#5182cf').grid(row=3,column=0)
    Label_temp = Label(root, background='#5182cf')
    Label_temp.grid(row=3,column=1)

    Labelws = Label(root, text="Wind Speed", background='#5182cf').grid(row=4,column=0)
    Label_ws = Label(root, background='#5182cf')
    Label_ws.grid(row=4,column=1)

    Labelstatus = Label(root, text="Status", background='#5182cf').grid(row=5,column=0)
    Label_status = Label(root, background='#5182cf')
    Label_status.grid(row=5,column=1)


def RealWeather():
    # Not strickly necessary, but good documentation.
    global Label_loc, Label_temp, Label_ws, Label_status

    try:
        api_request = requests.get("http://api.openweathermap.org/data/2.5/weather?appid=c2fc19de6fcd6869c4a9e167ee0f2eb7&q=" + city.get())
        api = json.loads(api_request.content)
        location = api['name'] + ", " + api['sys']['country']
        temp = int(api['main']['temp'] - 273.15)
        ws = api['wind']['speed']
        status = api['weather'][0]['main']

        Label_loc.config(text=location)
        Label_temp.config(text=temp)
        Label_ws.config(text=ws)
        Label_status.config(text=status)

    except Exception as e:
        print('error occurred:', e)


create_labels()
butn = Button(root, text="Search", command=RealWeather)
butn.grid(row=0, column=3)
Label1 = Label(root, text="Enter City: ")
Label1.grid(row=0,column=0)
city = Entry(root)
city.grid(row=0, column=1, padx=20)

root.mainloop()

附注我还强烈建议您阅读并开始遵循 PEP 8 - Style Guide for Python Code .

关于python - 如何在 tkinter 中更新文本标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65025684/

相关文章:

python - Tkinter 在 Pane 窗口中动态调整小部件的大小

python - 标签变量未在函数中设置

python - 如何从 Tcl 脚本调用 Python 函数

python - SelectById2 的指针标注

python - 开泰结构 : calculated instances with a condition

python - 从 Tkinter 的组合框中获取选定的值

python - 如何使字典在类实异常(exception)部不可变但在 python 中的类实例内部可变

python - 如何从 Django 的列表中删除 unicode 字符?

python - 将 lambda 函数绑定(bind)到使用 For 循环创建的多个 Tkinter Entry 小部件

python - 尝试清除 Tkinter Entry Widget 时出错