【发布时间】:2021-01-09 10:28:12
【问题描述】:
我正在开发一个天气应用程序并添加一些我正在考虑添加天气地图的香料,所以联系https://openweathermap.org/api/weathermaps 并获得了一个带有图像的 URL。我研究了许多在 Tkinter Widget 上显示该图像的方法,但它们都不起作用。它显示图像的大小,但不显示图像本身。这是我的代码。非常感谢。
from tkinter import *
from PIL import ImageTk, Image
import requests
import urllib.request
import base64
root = Tk()
root.title("Weather")
link = "https://tile.openweathermap.org/map/pressure_new/0/0/0.png?appid={APIkey}"
class WebImage:
def __init__(self,url):
u = urllib.request.urlopen(url)
raw_data = u.read()
u.close()
self.image = PhotoImage(data=base64.encodebytes(raw_data))
def get(self):
return self.image
img = WebImage(link_6).get()
imagelab = Label(root, image = img)
imagelab.grid(row = 0, column = 0)
root.mainloop()
【问题讨论】: