2

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.

bedipat
  • 21
  • 3
  • You cannot add a label with image to a column as it just insert the name of the label (that is what you see). However you can add an image to the *tree column* #0 using `image` option of `treev.insert(...)`. – acw1668 Oct 03 '22 at 10:24

0 Answers0