我正在开发一个天气应用程序,为了增加一些趣味性,我考虑添加一个天气地图,所以我访问了 https://openweathermap.org/api/weathermaps,并获取了一个图片的URL。我试图找到多种在Tkinter小部件中显示该图片的方法,但是它们都不起作用。它只显示图片的大小,但没有显示图片本身。这是我的代码。非常感谢。
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()
def get(self): return self.image img = WebImage(link).get() imagelab = tk.Label(root, image=img) imagelab.grid(row=1, column=0)
tk.Button(root, text = "提交", command = submit).grid(row = 0, column = 0) ` - Lorenzo Hsuimagelab.image = img
。 - acw1668imagelab
创建后。 - acw1668