如何防止图像被垃圾回收?

How to prevent images from garbage collection?

除最后一个图像外,所有图像都进入垃圾回收。如何显示所有图片?

        t = 1
        images = []
        while t < total_t[0][0]:
            row = self.database.db_query(F"SELECT * FROM {self.database.main_table} where T_ID = {t}")
            
            t_type = row[0][1]
            t_kind = row[0][2]
            t_note = row[0][3]
            t_date = row[0][4]
            t_ammount = row[0][5]
            
            self.img = tk.PhotoImage(file=f"{transactions_path}{t-1}.png")
            
            images.append(self.img)
            
            t+=1
            
        for index, image in enumerate(images):
            self.image = image
            tk.Label(self.content_view_frame, image=self.image).grid(row=index)

您需要保留对每张图片的引用。您看到最后一个是因为对那个的引用在 self.image 中。将 images 更改为 self.images,它应该可以工作。

干杯!