Python Forum
[Tkinter] Image does not show in treeview. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Image does not show in treeview. (/thread-18096.html)



Image does not show in treeview. - KevinBrown - May-05-2019

I'm trying to put images into treeviews.

Why does the image file Part.png not show up in this treeview.

I've loaded the same image ( Part.png) to show in the window and that works OK. are there some prerequisites for images on treeviews?



    tupbomi = [tuple(row) for row in all_rows]

    # How may items in this BOM
    print(len(tupbomi))
    numbomi = (len(tupbomi))

    for nbi in range(0,(numbomi)):
        tupbomd=tupbomi[nbi]
        print(( str(nbi)) + ' ' + tupbomd[1] + ' ' + tupbomd[2] + ' ' + tupbomd[3])

        img = tk.PhotoImage(file='resources/Part.png')
                # Check that image and path are correct by displaying on window
        window.tk.call('wm', 'iconphoto', window._w, PhotoImage(file='resources/Part.png'))
                # OK image shows

        # This will be BOM
        if tupbomd[2]== 'D':
            nbi= tree1.insert("", nbi, str(nbi), text=tupbomd[3], image=img , values=(tupbomd[2], tupbomd[3], tupbomd[4]))
            tree1.insert(nbi, "end", str(nbi)+'.1', text=tupbomd[3], image=img , values=(tupbomd[2], tupbomd[3], tupbomd[4]))

        # This will be a part
        else:
            tree1.insert("", nbi, text=tupbomd[3], image=img, values=( tupbomd[2], tupbomd[3], tupbomd[4]))



RE: Image does not show in treeview. - micseydel - May-05-2019

Can you give some context? Is there a library you're using? Is this a GUI question or a non-GUI question?


RE: Image does not show in treeview. - KevinBrown - May-05-2019

Context: I'm searching for matching records from an SQLite database.


the tuple tupbomi contains the fetchall results from that database.

Where the record tupbomd[2]== 'D': this is a node with further detail to be expanded.



Not a GUI question, however I suppose that in doing this I am writing my own window user interface (GUI).

I should say that in future the appropriate image will be identified by tupbomd[n], in the first instance I just want to understand how to load an image into a treeview.


RE: Image does not show in treeview. - KevinBrown - May-05-2019

While the following code is inside the function it will at least provide an image for the last item on the list. If the code is outside the function it provides the same image for all items in the treeview.

At runtime I want to do is read the required image for example tupbomd[n] and apply the appropriately rendered image to that particular line of the treeview.



 

        widt = 16
        hght = 16
        img = Image.open('resources/Part.png')
        img = img.resize((widt, hght), Image.ANTIALIAS)
        render = ImageTk.PhotoImage(img)
    for nbi in range(0,(numbomi)):

        tupbomd=tupbomi[nbi]

        widt = 16
        hght = 16
        img = Image.open('resources/Part.png')
        img = img.resize((widt, hght), Image.ANTIALIAS)
        render = ImageTk.PhotoImage(img)

        # This will be BOM
        if tupbomd[2]=='D':
            drgnumb = tupbomd[3]
            bom_read_drawing()
            nbi= tree1.insert("", nbi, str(nbi), text=tupbomd[3], image=render, values=(tupbomd[2], tupbomd[3], tupbomd[4] ,des1))
            tree1.insert(nbi, "end", str(nbi)+'.1', text=tupbomd[3], image=render, values=(tupbomd[2], tupbomd[3], tupbomd[4]))

        # This will be a part
        else:
            partnum = tupbomd[3]
            bom_read_part()
            tree1.insert("", nbi, text=tupbomd[3], image=render, values=(tupbomd[2], tupbomd[3], tupbomd[4],des1))


    conn.close()