Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python tkinter映像-创建一个新映像,而不是替换它_Python_Image_Tkinter - Fatal编程技术网

python tkinter映像-创建一个新映像,而不是替换它

python tkinter映像-创建一个新映像,而不是替换它,python,image,tkinter,Python,Image,Tkinter,我已经建立了一个简单的天气应用程序使用这个。 教程中关于天气状况的图像有一个错误,所以我试图自己解决它。 我已经成功地显示了图像,但每次新的搜索,而不是取代旧的它“加起来”。 非常感谢您的帮助! 以下是相关代码: def search(): city = city_text.get() weather = get_weather(city) if weather: location_lbl['text'] = '{}, {}'.format(wea

我已经建立了一个简单的天气应用程序使用这个。 教程中关于天气状况的图像有一个错误,所以我试图自己解决它。 我已经成功地显示了图像,但每次新的搜索,而不是取代旧的它“加起来”。 非常感谢您的帮助! 以下是相关代码:

    def search():
    city = city_text.get()
    weather = get_weather(city)
    if weather:
        location_lbl['text'] = '{}, {}'.format(weather[0], weather[1])
        temp_lbl['text'] = '{:.2f}°C {:.2f}°F'.format(weather[2], weather[3])
        weather_lbl['text'] = weather[5]
        img1 = ImageTk.PhotoImage(Image.open(
            f"C:/Users/Alon/PycharmProjects/Weather_app/weather_icons/{weather[4]}.png"))
        label = Label(image=img1)
        label.image = img1
        label['bg'] = '#ffffff'
        label.pack()
    else:
        messagebox.showerror('Error', 'Cannot find city: "{}"'.format(city))


app = Tk()
app.title("Weather app")
app.geometry('700x350')

city_text = StringVar()
city_entry = Entry(app, textvariable=city_text)
city_entry.pack()

search_btn = Button(app, text='Search weather', width=12, command=search)
search_btn.pack()

location_lbl = Label(app, text='', font=('bold', 20))
location_lbl.pack()

image = Label(app, bitmap='')
image.pack()

temp_lbl = Label(app, text='')
temp_lbl.pack()

weather_lbl = Label(app, text='')
weather_lbl.pack()
app.mainloop()

您不应在每次搜索中为天气图像创建新标签,只需更新
image
label:

def search():
city=city\u text.get()
天气=获取天气(城市)
如遇天气:
位置{lbl['text']='{},{}'。格式(天气[0],天气[1])
temp_lbl['text']='{.2f}°C{.2f}°F'。格式(天气[2],天气[3])
天气_lbl['text']=天气[5]
img1=ImageTk.PhotoImage(file=f“C:/Users/Alon/PycharmProjects/Weather_app/Weather_icons/{Weather[4]}.png”)
图像['image']=img1
image.image=img1
其他:
messagebox.batherRor('错误','找不到城市:“{}”'。格式(城市))

您不应该在每次搜索中为天气图像创建新标签,只需更新
图像
标签:

def search():
city=city\u text.get()
天气=获取天气(城市)
如遇天气:
位置{lbl['text']='{},{}'。格式(天气[0],天气[1])
temp_lbl['text']='{.2f}°C{.2f}°F'。格式(天气[2],天气[3])
天气_lbl['text']=天气[5]
img1=ImageTk.PhotoImage(file=f“C:/Users/Alon/PycharmProjects/Weather_app/Weather_icons/{Weather[4]}.png”)
图像['image']=img1
image.image=img1
其他:
messagebox.batherRor('错误','找不到城市:“{}”'。格式(城市))

非常感谢你,你成功了!我想我需要从一开始就学习tkinter。非常感谢你,我做到了!我想我需要从一开始就学习。。