1

Here is my code for inserting:

import tkinter as tk
from tkinter import ttk

class SummaryTree(ttk.Treeview):
    def __init__(self, parent, kwargs):
        ttk.Treeview.__init__(self, parent, columns=2, selectmode=tk.NONE, show='tree', takefocus=False)

        self.column('#1', anchor=tk.W)
        self.tag_configure('evenrow', background='#cecece')

        for index, item in enumerate(kwargs.items()):
            if index % 2 == 0:
                self.insert('', tk.END, text=item[0], values=item[1], tags=('evenrow',))
            else:
                self.insert('', tk.END, text=item[0], values=item[1])

if __name__ == '__main__':
    root = tk.Tk()
    kwargs = {
        'Soda': [
            'Sprite',
            'Mountain Dew',
            'Coke'
        ],
        'Numbers': [
            5,
            6,
            7
        ]
    }
    SummaryTree(root, kwargs).pack()
    root.mainloop()

For some reason this code is only inserting the first value in the lists. I'm not quite sure what I am missing here.

tristan957
  • 551
  • 2
  • 4
  • 14

1 Answers1

2

According to the ttk.Treeview documentation, the columns option is:

A list of column identifiers, specifying the number of columns and their names

So if you want your Treeview to have 3 columns named 1, 2, and 3, you should write:

ttk.Treeview.__init__(self, parent, columns=[1, 2, 3], ...)
Josselin
  • 2,593
  • 2
  • 22
  • 35
  • 1
    Ahh now I understand. I am just blind. Thanks for the help. Do you know how to keep the **text** item in a treeview from being focused so that it doesn't have the dashed lines around it? – tristan957 Jul 18 '17 at 15:18
  • 1
    You're welcome :) To remove the dashed lines on focus, try to edit the ttk style as shown here: [Removing Ttk Notebook Tab Dashed Line](https://stackoverflow.com/a/23399786/4084269) – Josselin Jul 18 '17 at 15:36
  • I understand the code but unfortunately I don't know the layout string to use. I have tried Treeview.Cell, Treeview.Column, Treeview.Text, and all of them report errors because they don't exist – tristan957 Jul 18 '17 at 16:21
  • 1
    I'm also not sure which one you should use. Maybe you can ask it as a new question... – Josselin Jul 18 '17 at 17:07