タイトルによれば、ダウンロードすることなくURLから画像を表示したい。私のコードは、これまで:URLから画像をtkinterに表示
from bs4 import BeautifulSoup as soup
from tkinter import *
import urllib.parse
from PIL import Image, ImageTk
import io
website = "http://www.porcys.com/review/"
openWebsite = soup(urllib.request.urlopen(website), 'html.parser')
reviews = openWebsite.find(name="section", attrs={'class': 'slider-content review'}).ul
results = []
for a in reviews(href=True):
temp = "http://www.porcys.com"+a['href']
results.append(temp)
mainWindow = Tk()
mainWindow.title("Latest Porcys reviews")
for i in range(0, 8):
review = results[i]
openReview = soup(urllib.request.urlopen(review), 'html.parser')
rating = openReview.find(name="span", attrs={'class': 'rating'})
album = openReview.find(name="div", attrs={'class': 'wrapper'}).i
artist = openReview.find(name="div", attrs={'class': 'wrapper'}).h2
coverWIP = openReview.find(name="img", attrs={'class': 'cover'})
for tag in openReview.find_all('i'):
tag.replaceWith(' ')
print(artist.text)
print(album.text)
print(rating.text)
cover = "http://www.porcys.com"+coverWIP['src']
print(cover)
artistAndAlbum = Label(font=("Helvetica",10), text=artist.text+'- '+album.text)
artistAndAlbum.grid(row=i, column=100, sticky=W)
ratingGUI = Label(font=("Helvetica",10), text=rating.text)
ratingGUI.grid(row=i+1,columnspan=100)
raw_data = urllib.request.urlopen(cover).read()
im = Image.open(io.BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
label1 = Label(mainWindow, image=image)
label1.grid(row=i, sticky=W)
mainWindow.mainloop()
「mainWindow.mainloop()」のトラブルを引き起こしている - ループ内で、それが唯一の最初の画像を表示し、未亡人を閉じた後、いくつかのエラーがスローされたとき。ループの外側に置くと、最後の画像だけが表示されます。また、画像を表示するのに最も効率的な方法であるかどうかもわかりません。
、PILインポートイメージから 'ImageTk'を忘れると'ピップはPyhton 3用pillow'をインストールしないでください – CGFoX