Skip to content
Advertisement

Treeview heading off by one column?

I cannot for the life of me find any post or documentation that explains why the Treeview heading is off by one column from the rest of the data.

Even the documentation I found shows this issue in an example but does not describe the issue.

import tkinter as tk
import tkinter.ttk as ttk
import random

n, m = 40, 10
table = []
for i in range(n):
    line = []
    # line.append('') 
    # This adds a blank string to start of row data to off set the data.
    # It is my current work around but does not look great.
    for j in range(m):
        line.append(random.randint(0, 999))
    table.append(line)


class Demo(tk.Tk):
    def __init__(self):
        super().__init__()

        tree = ttk.Treeview(self)
        tree.pack()
        for i in range(n):
            tree.insert('', 'end', text=table[i][0], values=table[i][1:])

        tree['columns'] = list(range(m - 1))
        headings = list('ABCDEFGHI')
        for j in range(m - 1):
            tree.column(j, width=50, anchor='e')
            tree.heading(j, text=headings[j])


if __name__ == '__main__':
    Demo().mainloop()

As you can see the headings are off by one column. I cannot figure out why this is or how to fix it properly.

I did manage a bad workaround that appends an empty string to the start of the row data so it lines up with the correct headings but this cannot be the correct or best way of fixing this.

Am I missing something here? Is this normal for Treeview?

enter image description here

Advertisement

Answer

As I know first column has special meaning – it can be used to display tree with nested elements – like on image in tkdoc – and it may need special method to set header.

    tree.heading('#0', text='Hello')

Result:

enter image description here

You may also use "#1", "#2", etc. to set other headers.

You can use f'#{j}' in loop.

    headings = list('ABCDEFGHIJ')
    for j in range(m):
        tree.column(f'#{j}', width=50, anchor='e')
        tree.heading(f'#{j}', text=headings[j])

enter image description here


Full working code:

import tkinter as tk
import tkinter.ttk as ttk
import random

n, m = 40, 10
table = []
for i in range(n):
    line = []
    # line.append('') 
    # This adds a blank string to start of row data to off set the data.
    # It is my current work around but does not look great.
    for j in range(m):
        line.append(random.randint(0, 999))
    table.append(line)


class Demo(tk.Tk):
    def __init__(self):
        super().__init__()

        tree = ttk.Treeview(self)
        tree.pack()
        for i in range(n):
            tree.insert('', 'end', text=table[i][0], values=table[i][1:])

        tree['columns'] = list(range(m - 1))
        headings = list('ABCDEFGHIJ')
        for j in range(m):
            tree.column(f'#{j}', width=50, anchor='e')
            tree.heading(f'#{j}', text=headings[j])

        #tree.heading('#0', text='Hello')

if __name__ == '__main__':
    Demo().mainloop()

Doc: Column identifiers

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement