0

我正在尝试使用以下代码通过其 URL 显示图像(这是我从谷歌获取的示例图像):

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()

我收到错误“没有名为 PIL 的模块”。我已经在命令提示符下下载了 PIL,并在代码中尝试了“import PIL”,但似乎没有任何效果。

任何帮助,将不胜感激

4

2 回答 2

0

pil 和枕头,在大多数情况下,python 3.7 和 3.8 版本不支持。如果您的代码没有错误。将您的项目解释器降级到 python 3.6。然后点安装枕头。

于 2020-07-31T11:04:19.133 回答
0

你的python版本是 3.8

pip3.8 install pil
pip3.8 install pillow
于 2020-07-31T15:24:14.813 回答