Package nltk :: Package draw :: Module table :: Class Table
[hide private]
[frames] | no frames]

Class Table

source code

object --+
         |
        Table

A display widget for a table of values, based on a MultiListbox widget. For many purposes, Table can be treated as a list-of-lists. E.g., table[i] is a list of the values for row i; and table.append(row) adds a new row with the given lits of values. Individual cells can be accessed using table[i,j], which refers to the j-th column of the i-th row. This can be used to both read and write values from the table. E.g.:

>>> table[i,j] = 'hello'

The column (j) can be given either as an index number, or as a column name. E.g., the following prints the value in the 3rd row for the 'First Name' column:

>>> print table[3, 'First Name']
John

You can configure the colors for individual rows, columns, or cells using rowconfig(), columnconfig(), and itemconfig(). The color configuration for each row will be preserved if the table is modified; however, when new rows are added, any color configurations that have been made for columns will not be applied to the new row.

Note: Although Table acts like a widget in some ways (e.g., it defines grid(), pack(), and bind()), it is not itself a widget; it just contains one. This is because widgets need to define __getitem__(), __setitem__(), and __nonzero__() in a way that's incompatible with the fact that Table behaves as a list-of-lists.

Instance Methods [hide private]
 
__init__(self, master, column_names, rows=None, column_weights=None, scrollbar=True, click_to_sort=True, reprfunc=None, cnf={}, **kw)
Construct a new Table widget.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

    Widget-like Methods
 
pack(self, *args, **kwargs)
Position this table's main frame widget in its parent widget.
source code
 
grid(self, *args, **kwargs)
Position this table's main frame widget in its parent widget.
source code
 
focus(self)
Direct (keyboard) input foxus to this widget.
source code
 
bind(self, sequence=None, func=None, add=None)
Add a binding to this table's main frame that will call func in response to the event sequence.
source code
 
rowconfigure(self, row_index, cnf={}, **kw) source code
 
columnconfigure(self, col_index, cnf={}, **kw) source code
 
itemconfigure(self, row_index, col_index, cnf=None, **kw) source code
 
bind_to_labels(self, sequence=None, func=None, add=None) source code
 
bind_to_listboxes(self, sequence=None, func=None, add=None) source code
 
bind_to_columns(self, sequence=None, func=None, add=None) source code
 
rowconfig(self, row_index, cnf={}, **kw) source code
 
columnconfig(self, col_index, cnf={}, **kw) source code
 
itemconfig(self, row_index, col_index, cnf=None, **kw) source code
    Table as list-of-lists
 
insert(self, row_index, rowvalue)
Insert a new row into the table, so that its row index will be row_index.
source code
 
extend(self, rowvalues)
Add new rows at the end of the table.
source code
 
append(self, rowvalue)
Add a new row to the end of the table.
source code
 
clear(self)
Delete all rows in this table.
source code
 
__getitem__(self, index)
Return the value of a row or a cell in this table.
source code
 
__setitem__(self, index, val)
Replace the value of a row or a cell in this table with val.
source code
 
__delitem__(self, row_index)
Delete the row_indexth row from this table.
source code
 
__len__(self)
Returns: the number of rows in this table.
source code
 
_checkrow(self, rowvalue)
Helper function: check that a given row value has the correct number of elements; and if not, raise an exception.
source code
 
column_index(self, i)
If i is a valid column index integer, then return it as is.
source code
 
hide_column(self, column_index) source code
 
show_column(self, column_index) source code
 
selected_row(self)
Return the index of the currently selected row, or None if no row is selected.
source code
 
select(self, index=None, delta=None, see=True) source code
 
sort_by(self, column_index, order='toggle')
Sort the rows in this table, using the specified column's values as a sort key.
source code
 
_sort(self, event)
Event handler for clicking on a column label -- sort by that column.
source code
    Table Drawing Helpers
 
_fill_table(self, save_config=True)
Re-draw the table from scratch, by clearing out the table's multi-column listbox; and then filling it in with values from self._rows.
source code
 
_get_itemconfig(self, r, c) source code
 
_save_config_info(self, row_indices=None, index_by_id=False)
Return a 'cookie' containing information about which row is selected, and what color configurations have been applied.
source code
 
_restore_config_info(self, cookie, index_by_id=False, see=False)
Restore selection & color configuration information that was saved using _save_config_info.
source code
 
_check_table_vs_mlb(self)
Verify that the contents of the table's _rows variable match the contents of its multi-listbox (_mlb).
source code
Class Variables [hide private]
    Table Drawing Helpers
  _DEBUG = False
If true, then run _check_table_vs_mlb() after any operation that modifies the table.
Instance Variables [hide private]
  _mlb
The multi-column listbox used to display this table's data.
  _rows
A list-of-lists used to hold the cell values of this table.
Properties [hide private]

Inherited from object: __class__

    Table as list-of-lists
  column_names
A list of the names of the columns in this table.
Method Details [hide private]

__init__(self, master, column_names, rows=None, column_weights=None, scrollbar=True, click_to_sort=True, reprfunc=None, cnf={}, **kw)
(Constructor)

source code 

Construct a new Table widget.

Parameters:
  • master (Tkinter.Widget) - The widget that should contain the new table.
  • column_names (list of str) - A list of names for the columns; these names will be used to create labels for each column; and can be used as an index when reading or writing cell values from the table.
  • rows (list of list) - A list of row values used to initialze the table. Each row value should be a tuple of cell values, one for each column in the row.
  • scrollbar (bool) - If true, then create a scrollbar for the new table widget.
  • click_to_sort (bool) - If true, then create bindings that will sort the table's rows by a given column's values if the user clicks on that colum's label.
  • reprfunc (function) - If specified, then use this function to convert each table cell value to a string suitable for display. reprfunc has the following signature:
    >>> reprfunc(row_index, col_index, cell_value) -> str

    (Note that the column is specified by index, not by name.)

  • cnf, kw - Configuration parameters for this widget's contained MultiListbox. See MultiListbox.__init__() for details.
Overrides: object.__init__

pack(self, *args, **kwargs)

source code 

Position this table's main frame widget in its parent widget. See Tkinter.Frame.pack() for more info.

grid(self, *args, **kwargs)

source code 

Position this table's main frame widget in its parent widget. See Tkinter.Frame.grid() for more info.

rowconfigure(self, row_index, cnf={}, **kw)

source code 

columnconfigure(self, col_index, cnf={}, **kw)

source code 

itemconfigure(self, row_index, col_index, cnf=None, **kw)

source code 

bind_to_labels(self, sequence=None, func=None, add=None)

source code 

bind_to_listboxes(self, sequence=None, func=None, add=None)

source code 

bind_to_columns(self, sequence=None, func=None, add=None)

source code 

rowconfig(self, row_index, cnf={}, **kw)

source code 

columnconfig(self, col_index, cnf={}, **kw)

source code 

itemconfig(self, row_index, col_index, cnf=None, **kw)

source code 

insert(self, row_index, rowvalue)

source code 

Insert a new row into the table, so that its row index will be row_index. If the table contains any rows whose row index is greater than or equal to row_index, then they will be shifted down.

Parameters:
  • rowvalue - A tuple of cell values, one for each column in the new row.

extend(self, rowvalues)

source code 

Add new rows at the end of the table.

Parameters:
  • rowvalues - A list of row values used to initialze the table. Each row value should be a tuple of cell values, one for each column in the row.

append(self, rowvalue)

source code 

Add a new row to the end of the table.

Parameters:
  • rowvalue - A tuple of cell values, one for each column in the new row.

__getitem__(self, index)
(Indexing operator)

source code 

Return the value of a row or a cell in this table. If index is an integer, then the row value for the indexth row. This row value consists of a tuple of cell values, one for each column in the row. If index is a tuple of two integers, (i,j), then return the value of the cell in the ith row and the jth column.

__setitem__(self, index, val)
(Index assignment operator)

source code 

Replace the value of a row or a cell in this table with val.

If index is an integer, then val should be a row value (i.e., a tuple of cell values, one for each column). In this case, the values of the indexth row of the table will be replaced with the values in val.

If index is a tuple of integers, (i,j), then replace the value of the cell in the ith row and jth column with val.

__len__(self)
(Length operator)

source code 
Returns:
the number of rows in this table.

column_index(self, i)

source code 

If i is a valid column index integer, then return it as is. Otherwise, check if i is used as the name for any column; if so, return that column's index. Otherwise, raise a KeyError exception.

hide_column(self, column_index)

source code 

show_column(self, column_index)

source code 

selected_row(self)

source code 

Return the index of the currently selected row, or None if no row is selected. To get the row value itself, use table[table.selected_row()].

select(self, index=None, delta=None, see=True)

source code 

sort_by(self, column_index, order='toggle')

source code 

Sort the rows in this table, using the specified column's values as a sort key.

Parameters:
  • column_index - Specifies which column to sort, using either a column index (int) or a column's label name (str).
  • order - Specifies whether to sort the values in ascending or descending order:
    • 'ascending': Sort from least to greatest.
    • 'descending': Sort from greatest to least.
    • 'toggle': If the most recent call to sort_by() sorted the table by the same column C({column_index}), then reverse the rows; otherwise sort in ascending order.

_fill_table(self, save_config=True)

source code 

Re-draw the table from scratch, by clearing out the table's multi-column listbox; and then filling it in with values from self._rows. Note that any cell-, row-, or column-specific color configuration that has been done will be lost. The selection will also be lost -- i.e., no row will be selected after this call completes.

_save_config_info(self, row_indices=None, index_by_id=False)

source code 

Return a 'cookie' containing information about which row is selected, and what color configurations have been applied. this information can the be re-applied to the table (after making modifications) using _restore_config_info(). Color configuration information will be saved for any rows in row_indices, or in the entire table, if row_indices=None. If index_by_id=True, the the cookie will associate rows with their configuration information based on the rows' python id. This is useful when performing operations that re-arrange the rows (e.g. sort). If index_by_id=False, then it is assumed that all rows will be in the same order when _restore_config_info() is called.

_check_table_vs_mlb(self)

source code 

Verify that the contents of the table's _rows variable match the contents of its multi-listbox (_mlb). This is just included for debugging purposes, to make sure that the list-modifying operations are working correctly.


Instance Variable Details [hide private]

_rows

A list-of-lists used to hold the cell values of this table. Each element of _rows is a row value, i.e., a list of cell values, one for each column in the row.

Property Details [hide private]

column_names

A list of the names of the columns in this table.

Get Method:
unreachable(self)