bedipat
bedipat

Reputation: 21

How to insert in a treeview some imgs saved in a list?

I have the next code:

from tkinter import ttk 
import tkinter as tk 
from PIL import Image, ImageTk

ID = [1,2,3,4,5, 6, 7, 8, 9]
Names = ['Tom', 'Rob', 'Tim', 'Jim', 'Kim', 'Steph', 'Davis', 'Katt']
photo = ['1.jpeg', '2.jpeg', '3.jpeg', '4.jpeg', '5.jpeg', '6.jpeg', '7.jpeg', '8.jpeg', '9.jpeg']
  
window = tk.Tk() 

treev = ttk.Treeview(window, selectmode ='browse') 
treev.pack(side='left',expand=True, fill='both') 
  

verscrlbar = ttk.Scrollbar(window,  
                           orient ="vertical",  
                           command = treev.yview) 
  
verscrlbar.pack(side ='right', fill ='y')   
treev.configure(yscrollcommand = verscrlbar.set) 

  
treev["columns"] = ('1', '2', '3') 

treev['show'] = 'headings'
  
treev.column("1", width = 90, anchor ='c') 
treev.column("2", width = 90, anchor ='c') 
treev.column("3", width = 90, anchor ='c') 


treev.heading("1", text ="ID") 
treev.heading("2", text ="Names") 
treev.heading("3", text ="photos") 

for i in range(len(photo)-1):
    img = Image.open(photo[i])
    tkimage = ImageTk.PhotoImage(img)
    
    treev.insert("", 'end', values =(ID[i], Names[i], tk.Label(window, image=tkimage)))
    i = i + 1
window.mainloop()

As you can see, I have a list with the path/name of the image I want to place in respective cell, I tried to do the next:

for i in range(len(photo)-1):
    img = Image.open(photo[i])
    tkimage = ImageTk.PhotoImage(img)
    
    treev.insert("", 'end', values =(ID[i], Names[i], tk.Label(window, image=tkimage)))
    i = i + 1

But when I run my code, I just get the next: Tkinter window And as you can see in photo column is just shown .!label<some number> and not the respective img.

I hope you can help me, thanks.

Upvotes: 2

Views: 95

Answers (0)

Related Questions