Ashish Gaurav
Ashish Gaurav

Reputation: 265

Remove empty first column of a Treeview object

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

Upvotes: 14

Views: 22109

Answers (5)

Dannyzimm
Dannyzimm

Reputation: 1

This worked for me:

tree['show'] = ''

Upvotes: 0

dralioz
dralioz

Reputation: 49

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

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

Upvotes: 2

Brueni92
Brueni92

Reputation: 137

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

Upvotes: 10

Demosthenex
Demosthenex

Reputation: 4451

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.

Upvotes: 39

joaquin
joaquin

Reputation: 85693

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.

Upvotes: 2

Related Questions