14

I'm trying to make a program that retrieves records from a database using sqlite3, and then display them using a Treeview.

I succeeded in having a table created with the records, but I just can't remove the first empty column.

def executethiscommand(search_str):
    comm.execute(search_str)
    records = comm.fetchall()
    rows = records.__len__()
    columns = records[0].__len__()

    win = Toplevel()
    list_columns = [columnames[0] for columnames in comm.description]
    tree = ttk.Treeview(win)
    tree['columns'] = list_columns

    for column in list_columns:
        tree.column(column, width=70)
        tree.heading(column, text=column.capitalize())

    for record in records:
        tree.insert("", 0, text="", values=record)

    tree.pack(side=TOP, fill=X)

enter image description here

buhtz
  • 10,774
  • 18
  • 76
  • 149
Ashish Gaurav
  • 265
  • 1
  • 2
  • 11

5 Answers5

38

That first empty column is the identifier of the item, you can suppress that by setting the show parameter.

t = ttk.Treeview(w)
t['show'] = 'headings'

That will eliminate that empty column.

Demosthenex
  • 4,343
  • 2
  • 26
  • 22
10

A little bit late and hacky but what you also could do is:

tree.column("#0", width=0)

not forgetting to set the minimum width too to zero by using; tree.column("#0", minwidth="0")

'#0' is the identifier of the first column, so setting the width to 0 would technically hide it

Community
  • 1
  • 1
Brueni92
  • 137
  • 1
  • 9
2
tree.column("#0", width = 0, stretch = "no")

With that, you can get rid of the first column.

Flair
  • 2,609
  • 1
  • 29
  • 41
dralioz
  • 49
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 21 '21 at 16:30
2

Probably you want to use something like a TkTable better than a TreeView.
In TreeView, the first column is defined for giving a name or id to the object described in each row. From the docs:

A treeview widget can display and allow browsing through a hierarchy of items, and can show one or more attributes of each item as columns to the right of the tree.

You fill the first column with:

tree.insert('', insert_mode, text='name first col')

If you still want to use the first column as a normal column you could try:

tree['columns'] = list_columns[1:]
for record in records:
    tree.insert("", 0, text=record[0], values=record[1:])

However I dont know how or even if it is possible also to fill the heading for this first column in the TreeView.

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • thanks joaquin, but can you please tell me how to allot the name of that empty column as well? thanks for your help:) – Ashish Gaurav Dec 31 '11 at 16:42
  • tree.heading('#0', text="First Column Heading" The above code would put a heading to the first column. – kmasif Sep 11 '19 at 14:59
0

This worked for me:

tree['show'] = ''
Dannyzimm
  • 1
  • 1