簡體   English   中英

在 tkinter 中清除屏幕后仍顯示文本

[英]Text still showing up after clearing screen in tkinter

我正在創建一個天氣應用程序,並且在某種程度上一切正常。 我輸入城市,按回車,然后它給我信息,然后我點擊明天的預測按鈕,它給我明天的預測。 然后,當我按下 go 后退按鈕時,它應該清除屏幕並帶我回到第一頁。 似乎是這樣做的,因為在按下 go 后它會清除屏幕,我可以獲取不同城市的信息,但是當我按下新城市的明天預測時,它仍然具有上一個條目的所有信息和明天的預測新城市沒有出現。

import sys
import tkinter as tk
from tkinter import font
import requests
from PIL import ImageTk, Image 
import time

window = tk.Tk()
window.geometry('750x500')
#window.resizable(width=False, height=False)

def opening():

    def format_response(weather):
        try:
            name = weather['city']['name']
            temperature_today = weather['list'][0]['main']['temp']
            desc = weather['list'][0]['weather'][0]['description']
            temp_feels = weather['list'][0]['main']['feels_like']
            humidity = weather['list'][0]['main']['humidity']
            wind_speed = weather['list'][0]['wind']['speed']

            final_str = f'City: {name} \nTemperature now: {temperature_today}°C --- feels like: {temp_feels}°C \nCondtions: {desc} \nHumidty: {humidity}% \nWind Speed: {wind_speed}m/s'

        except:
            final_str = 'There was a problem retrieving that information'

        return final_str

    def get_weather(city):

        weather_key = 'a9f553857df50650433c1577e3be6538'
        url = 'http://api.openweathermap.org/data/2.5/forecast'
        params = {'q': city, 'appid': weather_key, 'units': 'metric'}
        response = requests.get(url, params=params)
        weather = response.json()
        label_below['text'] = format_response(weather)

        temp_tmr = tk.Button(second_frame, font=('Bookman Old Style', 10), text="Tomorrow's Forecast", command=tomorrow)
        temp_tmr.place(relx=0.74, rely=0.88, relwidth=0.25, relheight=0.1)

    def resize_image(event):
        new_width = event.width
        new_height = event.height
        image = copy_of_image.resize((new_width, new_height))
        photo = ImageTk.PhotoImage(image)
        label.config(image = photo)
        label.image = photo


    image = Image.open('img\clouds.jpg')
    copy_of_image = image.copy()
    photo = ImageTk.PhotoImage(image)
    label = tk.Label(window, image = photo)
    label.bind('<Configure>', resize_image)
    label.pack(fill=tk.BOTH, expand = True)

    top_frame = tk.Frame(window, bg="#0cc961", bd=5)
    top_frame.place(relx=0.1, rely=0.05, relwidth=0.8, relheight=0.15)

    city_entry = tk.Entry(top_frame, font=('Bookman Old Style', 22))
    city_entry.place(relwidth=0.65, relheight=1)

    enter_button = tk.Button(top_frame, font=('Bookman Old Style', 18), text="Enter", command=lambda: get_weather(city_entry.get()))
    enter_button.place(relx=0.68, relwidth=0.31, relheight=1)

    second_frame = tk.Frame(window, bg="#0cc961", bd=5)
    second_frame.place(relx=0.1, rely=0.25, relwidth=0.8, relheight=0.7)

    label_below = tk.Label(second_frame, font=('Bookman Old Style', 15), anchor="nw", justify="left", bd=4)
    label_below.place(relwidth=1, relheight=1)

    return second_frame, city_entry

opening()

second_frame, city_entry = opening()

def tomorrow():
    second_frame = tk.Frame(window, bg="#0cc961", bd=5)


    for widget in second_frame.winfo_children():
        widget.place_forget()

    def format_response2(weather):
        try:
            name2 = weather['city']['name']
            temperature_tmr = weather['list'][1]['main']['temp']
            desc_tmr = weather['list'][1]['weather'][0]['description']
            temp_feels_tmr = weather['list'][1]['main']['feels_like']
            humidity_tmr = weather['list'][1]['main']['humidity']
            wind_speed_tmr = weather['list'][1]['wind']['speed']

            final_str = f'City: {name2} \nTemperature tomorrow: {temperature_tmr}°C --- will feel like: {temp_feels_tmr}°C \nCondtions: {desc_tmr} \nHumidty: {humidity_tmr}% \nWind Speed: {wind_speed_tmr}m/s'
        except:
            final_str = 'There was a problem retrieving that information'

        return final_str


    def goback():
        for widget in second_frame.winfo_children():
            widget.place_forget()
    
        opening()

    second_frame.place(relx=0.1, rely=0.25, relwidth=0.8, relheight=0.7)

    label_below2 = tk.Label(second_frame, font=('Bookman Old Style', 15), anchor="nw", justify="left", bd=4)
    label_below2.place(relwidth=1, relheight=1)

    temp_yest = tk.Button(second_frame, font=('Bookman Old Style', 10), justify="left", text="Go back", command=goback)
    temp_yest.place(relx=0.01, rely=0.88, relwidth=0.12, relheight=0.1)

    weather_key2 = 'a9f553857df50650433c1577e3be6538'
    url2 = 'http://api.openweathermap.org/data/2.5/forecast'
    params2 = {'q': city_entry.get(), 'appid': weather_key2, 'units': 'metric'}
    response2 = requests.get(url2, params=params2)
    weather2 = response2.json()
    label_below2['text'] = format_response2(weather2)
        
window.mainloop()

你不能把文字清空嗎?

def goback():
  for widget in second_frame.winfo_children():
    if isinstance(widget, tk.Label):
      widget["text"] = ""
    elif isinstance(widget, tk.Entry):
      widget.delete(0, 'end')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM