Python Forum
[Tkinter] Image does not show in treeview.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Image does not show in treeview.
#1
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]))
Reply
#2
Can you give some context? Is there a library you're using? Is this a GUI question or a non-GUI question?
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Reply
#3
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.
Reply
#4
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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020