简体   繁体   中英

When trying to display an image with a URL on tkinter, i get the error that there is no module named PIL

I am trying to display an image via its URL with the following code (this is an example image i took from google):

from tkinter import *
import pyrebase
from io import BytesIO
import requests
from PIL import ImageTk, Image
import os

multiQ = Tk()
multiQ.title("Multiple-Choice Question")
multiQ.resizable(0,0)

header = LabelFrame(multiQ, bg="white")
content = LabelFrame(multiQ, bg="white")

header.columnconfigure(0, weight=1)
homeButton=Button(content,width=50,height=50)
try:
    homeIcon=PhotoImage(file="yes.png")
    homeButton.config(image=homeIcon)
    homeButton.image = homeIcon
except TclError:
    print("Home")
homeButton.grid(row=1, sticky="w", padx=15, pady=2)

#this is the bit of code where i want to display the image and an error is occuring
my_page = urlopen("https://www.google.com/url?sa=i&url=https%3A%2F%2Ftowardsdatascience.com%2F3-numpy-image-transformations-on-baby-yoda-c27c1409b411&psig=AOvVaw18g0gcyX-PS9EMi0WxOhGT&ust=1596279067474000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCMDnw5Op9-oCFQAAAAAdAAAAABAD")
my_picture = io.BytesIO(my_page.read())
pil_img = Image.open(my_picture)
tk_img = ImageTk.PhotoImage(pil_img)
titleHeader = Label(content, text="Question Image here",pady=15, padx=20, bg="white", font=("Ariel",20, "bold"), anchor="w", relief="solid", image=tk_img)
titleHeader.grid(row=2, column=0, columnspan=4, padx=15, pady=5, ipadx=350, ipady=225)


aButton = Button(content, text="A", font=("Ariel",35, "bold"), borderwidth=2, relief="solid", activebackground="#12a8e3", bg="#12a8e3")
aButton.grid(row=3, column=0, padx=5, pady=(25,50), ipadx=30, ipady=20)

bButton = Button(content, text="B", font=("Ariel",35, "bold"), borderwidth=2, relief="solid", activebackground="#12a8e3", bg="#12a8e3")
bButton.grid(row=3, column=1, padx=5, pady=(25,50), ipadx=30, ipady=20)

cButton = Button(content, text="C", font=("Ariel",35, "bold"), borderwidth=2, relief="solid", activebackground="#12a8e3", bg="#12a8e3")
cButton.grid(row=3, column=2, padx=5, pady=(25,50), ipadx=30, ipady=20)

dButton = Button(content, text="D", font=("Ariel",35, "bold"), borderwidth=2, relief="solid", activebackground="#12a8e3", bg="#12a8e3")
dButton.grid(row=3, column=3, padx=5, pady=(25,50), ipadx=30, ipady=20)

header.grid(row=0, sticky='NSEW')
content.grid(row=1, sticky='NSEW')

multiQ.mainloop()

I get the error "No module named PIL". I have downloaded PIL in the command prompt and have tried "import PIL" in the code, but nothing seems to work.

Any help would be appreciated

pil and pillow, in most cases are not supported by python version 3.7 and 3.8. if your code are without bug. downgrade your project interpreter to python 3.6. then pip install pillow.

your python version is 3.8

pip3.8 install pil
pip3.8 install pillow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM