0

I have trouble how to display picture in a column of a treeview widget. Everythig works fine, until I wrapped the display operations in a function and try to fire it by buttom passing picture as a parameter (in dictionary).

How to fix it? Seems that picture goes to function properly but nothing is displayed. Without button and function readData program displays image in first column properly...

Here is a code:

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

root = Tk()
root.geometry("800x600")
root.title("App...")

styl = ttk.Style()
styl.configure("Treeview", rowheight=50)  # set row height


def readData(pic={}):
    img = Image.open(pic["pic"])
    img.thumbnail((50, 50))  # resize the image to desired size
    img = ImageTk.PhotoImage(img)
    var = []
    #some test data below
    for _ in range(5): 
        var.append("Automobile " + str(_))

    my_tree.insert(parent="", index="end", iid=0,
                   image=img, values=(var[0], var[1], var[2], var[3], var[4]))


my_tree = ttk.Treeview(root, selectmode="extended")

my_tree['columns'] = ("c1", "c2", "c3", "c4", "c5", "c6",)
my_tree.column("#0", minwidth=25, width=100, stretch=NO)
my_tree.column("c1", minwidth=25, width=100, anchor=CENTER)
my_tree.column("c2", minwidth=25, width=100, anchor=CENTER)
my_tree.column("c3", minwidth=25, width=100, anchor=CENTER)
my_tree.column("c4", minwidth=25, width=100, anchor=CENTER)
my_tree.column("c5", minwidth=25, width=100, anchor=CENTER)
# columns' headings
my_tree.heading("#0", text="Picture")
my_tree.heading("c1", text="Mark")
my_tree.heading("c2", text="Model")
my_tree.heading("c3", text="Year")
my_tree.heading("c4", text="Odo")
my_tree.heading("c5", text="Colour")
my_tree.pack()

pic = {}
pic = {"pic": 'zlom.png'}

displayButton = Button(root, text="Read data ...", command=lambda: readData(pic))
displayButton.pack(side=LEFT, padx=4, pady=4)

root.mainloop()

0 Answers0