繁体   English   中英

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

[英]Why are these images disappearing and how can I fix it (python)?

我目前正在程序中处理 Tkinter 和图像内容。 我想同时在屏幕上显示 9 张图像,但一次只有一张(最后一张)出现在屏幕上。 我已经隔离了这段代码中的问题,所以这是导致这个问题的代码的一部分。 函数“下载”从互联网接收图像,然后“ext”获取下载的图像并应该将它们放在屏幕上,但不是所有的都继续。

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()


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

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

  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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM