-
Notifications
You must be signed in to change notification settings - Fork 106
Usage
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
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)
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)
This needs to be called to update the display after programmatically changing the table contents:
table.redraw()
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()
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.
#by column name
table.sortTable(columnName='label')
#by column index
table.sortTable(columnIndex=1)
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)
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()
Column labels can be changed programmatically by accessing the columnlabels attribute of the table model:
table.model.columnlabels[colname] = newlabel
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.
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)
.
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.
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')