首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这些图像正在消失,我如何修复它(python)?

为什么这些图像正在消失,我如何修复它(python)?
EN

Stack Overflow用户
提问于 2021-07-30 03:30:15
回答 1查看 158关注 0票数 1

我目前正在一个程序中做Tkinter和图像方面的工作。我想同时在屏幕上显示9个图像,但是只有一个图像(最后一个)会同时出现在屏幕上。我在这段代码中隔离了问题,所以这是导致这个问题的代码的一部分。“下载”功能接收来自互联网的图像,然后"ext“获取下载的图像,并将它们放到屏幕上,但并不是所有的图像都继续运行。

代码语言:javascript
复制
import tkinter as tk
from tkinter.ttk import *
from threading import *
import random
from io import BytesIO
import urllib
import urllib.request
from PIL import Image, ImageTk


    
    
class CardClass:

  def __init__(self):
    self.master = tk.Tk()
    self.master.attributes('-zoomed', True) 
    self.frame = Frame(self.master)
    self.frame.pack()
    self.state = False
    self.master.attributes("-fullscreen", True) 
    self.but = Button(self.master, text="Print Images", command=self.Hand, pad = 150)
    self.but.pack(pady = 12)
    self.cardsList = ["https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg", "https://post.medicalnewstoday.com/wp-content/uploads/sites/3/2020/02/322868_1100-800x825.jpg", "https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg", "https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg", "https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg", "https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg", "https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg","https://post.medicalnewstoday.com/wp-content/uploads/sites/3/2020/02/322868_1100-800x825.jpg","https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg","https://post.medicalnewstoday.com/wp-content/uploads/sites/3/2020/02/322868_1100-800x825.jpg","https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg","https://post.medicalnewstoday.com/wp-content/uploads/sites/3/2020/02/322868_1100-800x825.jpg","https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg","https://post.medicalnewstoday.com/wp-content/uploads/sites/3/2020/02/322868_1100-800x825.jpg","https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg","https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg","https://www.thoughtco.com/thmb/O8fvqeXnAtOZ_L4eQ-aCRFKou_I=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/atlantic-bottlenose-dolphin--jumping-high-during-a-dolphin-training-demonstration-154724035-59ce93949abed50011352530.jpg"]
  


  def Hand(self):
    self.but.destroy()
    self.newCard = random.sample(self.cardsList, k=9)
    self.slot1 = self.cardsList[0]
    self.slot2 = self.cardsList[1]
    self.slot3 = self.cardsList[2]
    self.slot4 = self.cardsList[3]
    self.slot5 = self.cardsList[4]
    self.slot6 = self.cardsList[5]
    self.slot7 = self.cardsList[6]
    self.slot8 = self.cardsList[7]
    self.slot9 = self.cardsList[8]
    self.ext()
    


    

    

  def download(self, url, ex, ey):
    print("here1")
    with urllib.request.urlopen(url) as u:
      raw_data = u.read()
    img = Image.open(BytesIO(raw_data))
    self.iimage = ImageTk.PhotoImage(img.resize((50,50), Image.ANTIALIAS))
    lala = tk.Label(image=self.iimage)
    lala.place(x=ex, y=ey)
    return self.iimage

  def ext(self):
  
    self.download(self.newCard[0], 50, 150)
    self.download(self.newCard[1], 100, 150)
    self.download(self.newCard[2], 150, 150)
    self.download(self.newCard[3], 200, 150)
    self.download(self.newCard[4], 150, 150)
    self.download(self.newCard[5], 300, 150)
    self.download(self.newCard[6], 350, 150)
    self.download(self.newCard[7], 400, 150)
    self.download(self.newCard[8], 450, 150)
    



     
  


    
    



if __name__ == '__main__':
    w = CardClass()
    w.master.mainloop()

有人知道为什么会这样吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-30 04:31:31

我想我不明白为什么会这样,但问题是,实际上,当函数结束时,ImageTk.PhotoImage实例就会消失。不应该是这样的。-- Label小部件应该保存一个保持其活力的引用,但是如果您将这些PhotoImage对象存储在一个列表中,如下所示,代码可以工作:

代码语言:javascript
复制
  def Hand(self):
    self.but.destroy()
    self.newCard = random.sample(self.cardsList, k=9)
    self.keep = []
    self.ext()

  def download(self, url, ex, ey):
    with urllib.request.urlopen(url) as u:
      raw_data = u.read()
    img = Image.open(BytesIO(raw_data)).resize((50,50),Image.ANTIALIAS)
    iimage = ImageTk.PhotoImage(img)
    Label(self.master, image=iimage).place(x=ex,y=ey)
    self.keep.append(iimage)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68585128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档