Adding Image to Treeview



Treeview showing images
We can show image in only first coloumn #0.
To match the image height we have to modify the style by adding rowheight.
style = ttk.Style()
style.configure('Treeview', rowheight=100)  # increase height 
We are creating 3 image objects by using PNG images. Here image paths are to be changed as per your local system.
img1=tk.PhotoImage(file = "E:\\testing\\blob\\photos\\1.png")  # Path to 1 image 
img2=tk.PhotoImage(file = "E:\\testing\\blob\\photos\\2.png") 
img3=tk.PhotoImage(file = "E:\\testing\\blob\\photos\\3.png") 
from tkinter import ttk
import tkinter as tk
# Creating tkinter my_w
my_w = tk.Tk()
my_w.geometry("380x380") 
my_w.title("www.plus2net.com")  
# Using treeview widget style 
style = ttk.Style()
style.configure('Treeview', rowheight=100)  # increase height 

trv = ttk.Treeview(my_w, selectmode ='browse',height=3)  
trv.grid(row=1,column=1,rowspan=5,padx=30,pady=20)
# number of columns
trv["columns"] = ("1")
# Defining heading
trv['show'] = 'tree headings'
#trv['show'] = 'tree'

trv.column("#0", width = 150, anchor ='center') # Width of column
trv.column("1", width = 130, anchor ='center')
# Headings  
# respective columns
trv.heading("#0", text ="#") # Text of the column 
trv.heading("1", text ="Name")

img1=tk.PhotoImage(file = "E:\\testing\\blob\\photos\\1.png")  # Path to 1 image 
img2=tk.PhotoImage(file = "E:\\testing\\blob\\photos\\2.png") 
img3=tk.PhotoImage(file = "E:\\testing\\blob\\photos\\3.png") 

trv.insert("",'end',iid='1',open=True,text='1',image=img1,values=('na-Alex'))
trv.insert("",'end',iid=2,open=True,text='2',image=img2,values=('n1-Alex'))
trv.insert("",'end',iid='3',open=True,text='3',image=img3,values =('Child-Alex'))

my_w.mainloop()

Using JPG images

We have to use PIL library
from PIL import Image,ImageTk
We are replacing the first row image with the JPG image. ( All other lines remain same ).
img1 = ImageTk.PhotoImage(Image.open("E:\\testing\\blob\\photos\\1.jpg"))

Generating Invoice by adding products using Treeview Adding row to Treeview after inserting to MySQL table
Treeview Records of MySQL Pagination of Records of MySQL Delete Records
Tkinter
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer