当试图在tkinter上显示带有URL的图像时,我得到一个错误,即没有名为PIL的模块

2024-04-25 20:57:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过URL显示一幅图像,代码如下(这是我从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()

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

任何帮助都将不胜感激


Tags: textimageimportbuttoncontentgridrowbg
2条回答

你的python版本是3.8

pip3.8 install pil
pip3.8 install pillow

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

相关问题 更多 >

    热门问题