0

having problems displaying an image using treeview. Found some other mentions on the net with a similar problem but the answers do not seem to work for me. I am using Win10 if that makes a difference

import ttkthemes
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from tkinter import PhotoImage
from PIL import Image, ImageTk


self = tk.Tk()
self.title('Tkinter PhotoImage Demo')

#self.image = Image.open("A4.bmp")
#self.python_image = ImageTk.PhotoImage(self.image)
#print(self.python_image)
#ttk.Label(self, image=self.python_image).pack()



# columns
columns = ('#1', '#2')

tree = ttk.Treeview(self, columns=columns, show='headings', height=20)
tree.tag_configure('oddrow', background='#ece0cf')
tree.tag_configure('evenrow', background='#e0e0e0')
style = ttk.Style()
style.theme_use('clam')
style.configure("Treeview",font=(None,12))
style.configure("Treeview.Heading", font=(None, 12))

# define headings
tree.heading('#1', text='Date')
tree.column("#1", minwidth=0, width=160)
tree.heading('#2', text='Logo')
tree.column("#2", minwidth=0, width=80)


# add a scrollbar
scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')
tree.grid(row=0, column=0, sticky='nsew')

logo="A4.bmp"
rowcol='oddrow'

im = Image.open(logo)
ph = ImageTk.PhotoImage(im)
print(ph)
tree.insert('', -1, values=("2021-03-25", logo), image=ph, tags=(rowcol,))
tree.grid(row=0, column=0, sticky='nsew')
self.update_idletasks()
self.update()

If it helps the value of ph is: pyimage1 The commented out code at the start displays the image just fine, so the issue seems to be on adding the image itself into the tree.insert.... bit of coding

Whilst I have your attention and as related but very minor questions, is there also a way to add a second image into treeview? Plus is there a way to display some values, then an image, then some more values, then another image and finally more values, or do the images have to be at the start or end of the rows?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Alexis
  • 11
  • 4
  • Are the other values being shown? Since I see a `self`. Is this inside a function(or class)? Then keep a reference to item by saying `self.ph`. – Delrius Euphoria Apr 25 '21 at 18:21
  • Yes the values are being displayed just fine, it is only the image that is not. It will be inside a class and thanks for mentioning that, but right now the code is exactly as is written, but still nowt. – Alexis Apr 25 '21 at 18:32
  • I did just try putting the image into the header and that works fine `code`tree.heading('#1', text='Date') replaced with `code`tree.heading('#1', text='Date', image=ph) So the issues seems to be on the tree.insert line `code`tree.insert('', -1, values=("2021-03-25", logo), image=ph, tags=(rowcol,)) – Alexis Apr 25 '21 at 18:55
  • `text` and `image` options are shown in the `tree` column (`#0`) which you have disabled by the `show='headings'` option in `Treeview(...)`. – acw1668 Apr 26 '21 at 09:20
  • Wonderful, thanks very much for that, and that also answered my additional questions :) – Alexis Apr 26 '21 at 14:14

0 Answers0