0
votes

I know how to add an icon in the #0 column using the tags keyword :

icon = ImageTk.PhotoImage(Image.open('path/to/icon.gif')
myTreeView.tag_configure('tag_with_this_icon', image=icon)

# some loop
myTreeView.insert( [...],tags='tag_with_this_icon')

I would need to add an additional icon, or add an icon in any other column of the treeview, is it possible with tkinter ?

or do I need to tweak my icon gif files with two icons in the same icon file (all possible permutation)

or stich another treeview to the left of the first treeview with one column/empty text jsu for a second icon ? seems a bad hack... any better ideas appreciated, thanks.

1

1 Answers

0
votes

A way would be to use GTK3 rather than tkinter, a valid example below. OP ask for a solution with tkinter, but OP is me and did not find its way with it, so...

from gi.repository import Gtk, Gdk, GdkPixbuf

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, str, str)
        self.treeview = Gtk.TreeView(model=self.liststore)

        self.liststore.append(["dot_red16x16.png", "This is a symbol1","dot_green16x16.png"])

        px_renderer = Gtk.CellRendererPixbuf()
        px_column = Gtk.TreeViewColumn('')
        px_column.pack_start(px_renderer, False)

        str_renderer = Gtk.CellRendererText()
        px_column.pack_start(str_renderer, False)

        px_renderer2 = Gtk.CellRendererPixbuf()
        px_column.pack_start(px_renderer2, False)

        # set data connector function/method
        px_column.set_cell_data_func(px_renderer, self.get_tree_cell_pixbuf,0)
        px_column.set_cell_data_func(px_renderer2, self.get_tree_cell_pixbuf,2)
        px_column.set_cell_data_func(str_renderer, self.get_tree_cell_text)
        self.treeview.append_column(px_column)

        self.add(self.treeview)

    def get_tree_cell_text(self, col, cell, model, iter, user_data):
        cell.set_property('text', model.get_value(iter, 1))

    def get_tree_cell_pixbuf(self, col, cell, model, iter, user_data):
        cell.set_property('pixbuf', GdkPixbuf.Pixbuf.new_from_file(model.get_value(iter, user_data)))

if __name__ == '__main__':
    win = MyWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()

line 11, replace dot_red16x16.png and dot_green16x16.png names with your own icon files and save this snipped aside the icon files.

gtk readthedocs : https://python-gtk-3-tutorial.readthedocs.io

side note : actually the harder (on windows) is to setup GTK3 to work. Here is two ways I tested :