Vipul Tyagi
Vipul Tyagi

Reputation: 112

How to prevent images from garbage collection?

All images are getting into garbage collection except the last one. How to show all images?

        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)

Upvotes: 0

Views: 583

Answers (1)

Pietro
Pietro

Reputation: 1110

You need to keep a reference to each image. You see the last one because a reference to that one is inside self.image. Change images to self.images and it should work.

Cheers!

Upvotes: 1

Related Questions