使用
按顺序添加jpg图像没有问题image1 = Image.open("file_path")
photo1 = ImageTk.PhotoImage(image1)
text.image_create(END, image=photo1)
image2 = Image.open("file_path")
photo2 = ImageTk.PhotoImage(image2)
text.image_create(END, image=photo2)
....
但是当我尝试从文本文件中显示文本和图像时,如:
.... lines .....
<img>file_path to image 1</img>
.... lines .....
<img>file_path for image 2</img>
.... lines .....
<img>file_path for image 3</img>
.... lines .....
使用循环:
with open(file_name) as f:
for line in f:
if line[0:5] == "<img>":
file_path = line[5:-7]
image = Image.open(file_path)
photo = ImageTk.PhotoImage(image)
text.image_create(END, image=photo)
txt.insert(END, '\n')
我只获得文字行而不是图像空白区域。我做错了什么?
答案 0 :(得分:0)
图像可能被垃圾收集器破坏了。保存对图像的引用。例如:
images = []
with open(file_name) as f:
for line in f:
if line[0:5] == "<img>":
...
images.append(photo)
...