0

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

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • as I know first column has special meaning - it can be used to display tree with nested elements - like on image in [tkdoc](https://tkdocs.com/tutorial/tree.html) - and it may need special method to set header. – furas May 01 '21 at 04:39
  • probably `tree.heading('#0', text='Name')` – furas May 01 '21 at 04:41
  • Set `show="headings"` in `Treeview(...)`. – acw1668 May 01 '21 at 17:35

1 Answers1

1

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

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you. The `f'#{j}'` solution will likely be what I need. I will work on this Monday when I am back at work. Initially I did all my work using labels and it worked fine but when adding several hundred rows and more than 30 columns it started slowing things down so I needed to switch to treeview. – Mike - SMT May 01 '21 at 18:33
  • if you woul dkeep data in `pandas.DataFrame` then you could use [pandastable](https://pandastable.readthedocs.io/en/latest/examples.html). It was used to build [DataExplorer](https://pandastable.readthedocs.io/en/latest/dataexplore.html) - there are images to see what you could build with `pandastable` – furas May 01 '21 at 19:34
  • That does look preferable. Can a pandas table be loaded into tkinter frames? – Mike - SMT May 01 '21 at 22:05
  • module `pandastable` is build using `tkinter` specially to use with `tkinter` - I have examples on my blog [Tkinter PandasTable Examples](https://blog.furas.pl/python-tkinter-pandastable-examples-gb.html) because official documentation has only basic example in [Code Examples](https://pandastable.readthedocs.io/en/latest/examples.html) – furas May 01 '21 at 22:25
  • Oh that looks exciting. I work with large datasets and this Pandas tables looks to be very useful. Thank you very much for the links and help :D – Mike - SMT May 01 '21 at 23:17