Delete empty first column of Treeview object

I am trying to create a program that retrieves records from a database using sqlite3 and then displays them using Treeview .

I managed to create a table with records, but I just can’t delete 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

+4
source share
3 answers

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

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

This will eliminate this empty column.

+17
source

A bit late and hacked, but what you can also do is:

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

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

+2
source

Perhaps you want to use something like TkTable better than TreeView.
In TreeView, the first column is defined to indicate the name or identifier for the object described on each row. From docs :

The treeview widget can display and allow viewing through a hierarchy of elements and can display one or more attributes of each element in the form of columns to the right of the tree.

Fill in the first column:

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

If you still want to use the first column as a regular column, you can try:

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

However, I do not know how or even if it is possible to also fill in the header for this first column in the TreeView.

+1
source

Source: https://habr.com/ru/post/1388732/


All Articles