首页 > 解决方案 > How to display image in python Tkinter from url

问题描述

How to display image of "https://upload.wikimedia.org/wikipedia/commons/a/a0/Bill_Gates_2018.jpg" this url in my tkinter project. is there is any way to do that.

标签: python-3.xtkinter

解决方案


urllib.requrest.urlopen()下面是一个使用andPillow模块从 URL 显示图像的简单示例:

from urllib.request import urlopen
import tkinter as tk
from PIL import ImageTk

image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Bill_Gates_2018.jpg"

root = tk.Tk()
data = urlopen(image_url)
image = ImageTk.PhotoImage(data=data.read())
tk.Label(root, image=image).pack()
root.mainloop()

推荐阅读