我有这段代码:
for imgTag in outputS:
print(imgTag["src"])
root = tk.Tk()
for imgTag in outputS:
url = imgTag["src"]
with urllib.request.urlopen(url) as u:
raw_data = u.read()
im = Image.open(BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
image.im = image
label = tk.Label(root, image=image)
label.pack( side = tk.BOTTOM)
root.mainloop()
它在命令行中生成:
http://images.sportinglife.com/racing/tsilks/92913.png
http://images.sportinglife.com/racing/tsilks/320898.png
http://images.sportinglife.com/racing/tsilks/6962.png
http://images.sportinglife.com/racing/tsilks/351124.png
http://images.sportinglife.com/racing/tsilks/239668.png
http://images.sportinglife.com/racing/tsilks/375038.png
http://images.sportinglife.com/racing/tsilks/201969.png
http://images.sportinglife.com/racing/tsilks/441.png
http://images.sportinglife.com/racing/tsilks/2384.png
http://images.sportinglife.com/racing/tsilks/363428.png
http://images.sportinglife.com/racing/tsilks/1828.png
http://images.sportinglife.com/racing/tsilks/226478.png
http://images.sportinglife.com/racing/tsilks/4966.png
http://images.sportinglife.com/racing/tsilks/375018.png
http://images.sportinglife.com/racing/tsilks/17484.png
然而,有些图像显示在tkinter窗口中,有些则没有。对于每个png链接列表,我在那里看起来似乎不是一个可呈现的可呈现模式,哪些没有,尽管对于每个列表,相同的那些总是被呈现。例如:list1 = [1,2,3,4,5],list 2 = [1,2,3,4,5]。列表1将始终呈现图像1,3,4,列表2将始终呈现2,3。
在另一个python文件中我有:
for x in range(0, num_horses):
row1 = tk.Frame(root)
row1.pack(side = tk.TOP)
label = tk.Label(row1, text="horse " + str(x + 1))
label.pack(side = tk.LEFT)
imgTag = outputS[x]
url = imgTag["src"]
with urllib.request.urlopen(url) as u:
raw_data = u.read()
im = Image.open(BytesIO(raw_data))
image = ImageTk.PhotoImage(im)
image.im = image
label = tk.Label(row1, image=image)
label.pack( side = tk.LEFT)
b1 = tk.Button(row1, text="back", command = lambda x=x: backHorse(x + 1))
b1.pack( side = tk.LEFT )
b2 = tk.Button(row1, text="lay", command = lambda x=x: layHorse(x + 1))
b2.pack( side = tk.LEFT )
当在与第一个python相同的链接列表上运行时,它们会根据呈现的图像产生不同的结果,但它们对于应用程序始终是相同的。例如:在列表[1,2,3,4,5]上,我在这篇文章中展示的第一个代码片段始终显示图像1,2,4,第二个代码始终显示图像2,5
答案 0 :(得分:0)
garbage collector
可能存在一些问题,可能会删除PhotoImage
,因此您必须分配给变量。
但不是
image.im = image
没有意义。
你需要
label.im = image
适用于我的完整版
import tkinter as tk
import urllib.request
from PIL import Image, ImageTk
from io import BytesIO
data = [
'http://images.sportinglife.com/racing/tsilks/92913.png',
'http://images.sportinglife.com/racing/tsilks/320898.png',
'http://images.sportinglife.com/racing/tsilks/6962.png',
'http://images.sportinglife.com/racing/tsilks/351124.png',
'http://images.sportinglife.com/racing/tsilks/239668.png',
'http://images.sportinglife.com/racing/tsilks/375038.png',
'http://images.sportinglife.com/racing/tsilks/201969.png',
'http://images.sportinglife.com/racing/tsilks/441.png',
'http://images.sportinglife.com/racing/tsilks/2384.png',
'http://images.sportinglife.com/racing/tsilks/363428.png',
'http://images.sportinglife.com/racing/tsilks/1828.png',
'http://images.sportinglife.com/racing/tsilks/226478.png',
'http://images.sportinglife.com/racing/tsilks/4966.png',
'http://images.sportinglife.com/racing/tsilks/375018.png',
'http://images.sportinglife.com/racing/tsilks/17484.png',
]
root = tk.Tk()
for url in data:
with urllib.request.urlopen(url) as u:
raw_data = u.read()
image = Image.open(BytesIO(raw_data))
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
label.photo = photo
label.pack()
root.mainloop()
顺便说一句:通常在函数或类中创建PhotoImage
时会出现问题,但在for
循环中可能存在同样的问题。