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.
2 Answers
Below is an simple example using urllib.requrest.urlopen() and Pillow module to show image from 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()
Comments
import urllib.request
import os
savein='test.jpg'
try:
urllib.request.urlretrieve('https://upload.wikimedia.org/wikipedia/commons/a/a0/Bill_Gates_2018.jpg',savein)
except:
print('Network Issue')
# then use the Tkinter code to use that savein as image address
# os.remove(savein) for deleting it later
I guess we can't use the image directly from the internet since we are using the Tkinter. So download the image and then use it in the Label and then delete it if u want.
savein variable has the link for the image