显示从 http 链接获取并在 tkinter 中显示的图像

Displaying a image which is fetched from a http link and displayed in tkinter

提问人:Abhishek Renjan 提问时间:11/14/2023 更新时间:11/14/2023 访问量:38

问:

我正在制作一个简单的天气应用程序来练习 tkinter。在此过程中,我遇到了一个主要问题,我得到的图像可以使用请求获取,并且在新的 tkinter 窗口中显示它是一个很大的问题。该程序仍未完成,但工作正常,但无法显示图像。

在 disp_weather() 中,您可以看到我创建了一个新窗口来显示详细信息,fw.get_icon() 获取 png 图像的 url,例如“//cdn.weatherapi.com/weather/64x64/day/176.png”,然后出现错误

raceback (most recent call last):
  File "C:\Users\Abhishek\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "c:\Users\Abhishek\Desktop\OIBSIP\proj3_weather\Init.py", line 23, in disp_weather
    imag.pack()
    ^^^^^^^^^

程序:

import fetch_weather as fw
from tkinter import *
import os
from PIL import Image, ImageTk
import requests
from io import BytesIO

def disp_weather():
    location=e1.get()
    unit=v.get()
    url=fw.get_icon()
    url="https:"+url
    new_window = Toplevel(root)
    new_window.geometry("300x200")
    new_window.title("New Window")
    
    response = requests.get(url) 
    img_data = BytesIO(response.content)
    img = Image.open(img_data)
    wImg = ImageTk.PhotoImage(img)
    imag = Label(new_window, image=wImg)  #Here is where the problem starts
    imag.photo = wImg
    imag.pack()
    

script_dir = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(script_dir, "weather.ico")
icon_path=icon_path.replace("\\", "/")

root = Tk()
root.title("Weather")
root.iconbitmap(default=icon_path)
root.geometry("400x300")

Label(root, text='Enter the Location:').grid(row=0, column=0, pady=4, sticky=W)

e1 = Entry(root)
e1.grid(row=0, column=1, pady=5, sticky=W)

v = IntVar()
Label(root,text='Choose a unit:').grid(row=1,column=0,pady=5,sticky=W)
Radiobutton(root, text='Celsius', variable=v, value=1).grid(row=1, column=1, pady=4, sticky=W)
Radiobutton(root, text='Fahrenheit', variable=v, value=2).grid(row=1, column=2, pady=4, sticky=W)

fetch_button = Button(root, text="Find", command=disp_weather)
fetch_button.grid(row=3, column=0, pady=10, columnspan=3)

mainloop()
python-3.x tkinter python-imaging-库

评论

0赞 acw1668 11/14/2023
错误回溯不完整。

答:

0赞 Andrej Kesely 11/14/2023 #1

我建议调试您的程序(例如,从服务器返回的实际内容是什么?将 URL 替换为 I 得到正确的结果:response"https://cdn.weatherapi.com/weather/64x64/day/176.png"

import os
from io import BytesIO
from tkinter import *

import requests
from PIL import Image, ImageTk


def disp_weather():
    location = e1.get()
    unit = v.get()
    url = "https://cdn.weatherapi.com/weather/64x64/day/176.png"
    new_window = Toplevel(root)
    new_window.geometry("300x200")
    new_window.title("New Window")

    response = requests.get(url)
    img_data = BytesIO(response.content)
    img = Image.open(img_data)
    wImg = ImageTk.PhotoImage(img)
    imag = Label(new_window, image=wImg)
    imag.photo = wImg
    imag.pack()


root = Tk()
root.title("Weather")
root.geometry("400x300")

Label(root, text="Enter the Location:").grid(row=0, column=0, pady=4, sticky=W)

e1 = Entry(root)
e1.grid(row=0, column=1, pady=5, sticky=W)

v = IntVar()
Label(root, text="Choose a unit:").grid(row=1, column=0, pady=5, sticky=W)
Radiobutton(root, text="Celsius", variable=v, value=1).grid(
    row=1, column=1, pady=4, sticky=W
)
Radiobutton(root, text="Fahrenheit", variable=v, value=2).grid(
    row=1, column=2, pady=4, sticky=W
)

fetch_button = Button(root, text="Find", command=disp_weather)
fetch_button.grid(row=3, column=0, pady=10, columnspan=3)

mainloop()

正确下载并显示图像:

enter image description here

评论

0赞 Abhishek Renjan 11/15/2023
是的,如果我只使用 url,它可以正常工作,但是当我尝试动态获取它时,它没有显示