Skip to content
insolor edited this page Aug 24, 2021 · 11 revisions

Import the class

The core classes are TableCanvas and TableModel. You will likely want to access the TableModel class to alter the data programmatically, otherwise the TableCanvas class is all that's required to add to the GUI. To import:

from tkintertable import TableCanvas, TableModel

or

from tkintertable.Tables import TableCanvas
from tkintertable.TableModels import TableModel

Create tables

To create a table, you typically create a frame in your applications GUI and provide this to the table constructor. Note that the createTableFrame method is used to add the table to the parent frame, so avoid using the pack or grid methods.

tframe = Frame(master)
tframe.pack()
table = TableCanvas(tframe)
table.show()

We can also create a model from some data, then use that model to initiate the table:

model = TableModel()
table = TableCanvas(frame, model=model)

Constructor options

You can pass the following arguments to the TableCanvas constructor:

table = TableCanvas(frame, model,
			cellwidth=60, cellbackgr='#e3f698',
			thefont=('Arial',12),rowheight=18, rowheaderwidth=30,
			rowselectedcolor='yellow', read_only=False)

Update the table

This needs to be called to update the display after programmatically changing the table contents:

table.redraw()

Get data into the table

It is possible to populate the tables by importing data from a csv file or creating a python dictionary. To import from a dictionary, it should be of the form:

data = {'rec1': {'col1': 99.88, 'col2': 108.79, 'label': 'rec1'},
       'rec2': {'col1': 99.88, 'col2': 108.79, 'label': 'rec2'}
       } 

Each record corresponds to a row in the dictionary. Columns will be created for each child key found in each record.

Then provide this dict to the constructor:

table = TableCanvas(frame, data=data)

Or we get a handle on the model (or we can create the model first and supply it as an argument to the table constructor):

model = table.model
model.importDict(data) 
table.redraw()

Importing from a text file

table.importCSV(filename, sep=',')

This can also be done interactively from the GUI by right clicking on the table and choosing 'Import Table' from the popup menu.

Sort the table

#by column name
table.sortTable(columnName='label')
#by column index
table.sortTable(columnIndex=1) 

Add and delete rows and columns

If column names are not given in the argument then a dialog will pop up in the GUI asking for the name which will likely not be what you want.

#add with automatic key
table.addRow()
#add with named key, other keyword arguments are interpreted as column values
table.addRow(keyname, label='abc')
#same as above with dict as column data
table.addRow(keyname, **{'label'='abc'})
table.addColumn(colname)
#delete Rows
table.deleteRows(range(0,2))
#delete Column
table.deleteColumn(colIndex)

Change data in individual cells

Simply get a handle on the table model dict and alter the data attribute (a dictionary) directly, then redraw the table.

data = table.model.data
cols = table.model.columnNames #get the current columns
data[row][col] = value #use row and column names, not cell coordinates
table.model.setValueAt(value,rowindex,colindex) ##use cell coords
table.redrawTable()

Change column labels

Column labels can be changed programmatically by accessing the columnlabels attribute of the table model:

table.model.columnlabels[colname] = newlabel

Row headers

The row header displays the row index number by default, but it can be used to show the row/record key names if necessary. You may also want to set the row header width to something larger than the default (40). Both these options are supplied in the constructor.

table = TableCanvas(master, model, rowheaderwidth=100, showkeynamesinheader=True)
You can hide the row header by setting rowheaderwidth=0.

Selecting rows

On events (e.g.) a button click, you can select a new row with table.setSelectedRow(row_number). Row numbers of a TableCanvas are 0-indexed, so the first row is row 0. To deselect/not select any row, you can use table.setSelectedRow(-1).

Jumping to rows

To jump to a specific row in the table, use table.set_yviews('moveto', y), where y is a float in [0, 1], where 0 is the absolute top, and 1 the absolute bottom.

Set preferences

Preferences for the table can be set on the constructor method, for example:

TableCanvas(frame, model=model,
               cellwidth=50, cellbackgr='#E3F6CE',
               thefont="Arial 10",rowheight=16,editable=False,
              rowselectedcolor='yellow',reverseorder=1)

Or can be loaded later from an external preferences file:

preferences=Preferences('TablesApp')
table.loadPrefs(preferences)
Save and load from a file
table.save('test.table')
table.load('test.table')