nltk.draw.table module

Tkinter widgets for displaying multi-column listboxes and tables.

class nltk.draw.table.MultiListbox[source]

Bases: tkinter.Frame

A multi-column listbox, where the current selection applies to an entire row. Based on the MultiListbox Tkinter widget recipe from the Python Cookbook (https://code.activestate.com/recipes/52266/)

For the most part, MultiListbox methods delegate to its contained listboxes. For any methods that do not have docstrings, see Tkinter.Listbox for a description of what that method does.

FRAME_CONFIG = {'background': '#888', 'highlightthickness': 1, 'takefocus': True}

Default configuration values for the frame.

LABEL_CONFIG = {'background': '#444', 'borderwidth': 1, 'font': 'helvetica -16 bold', 'foreground': 'white', 'relief': 'raised'}

Default configurations for the column labels.

LISTBOX_CONFIG = {'activestyle': 'none', 'borderwidth': 1, 'exportselection': False, 'highlightthickness': 0, 'selectbackground': '#888', 'selectborderwidth': 0, 'takefocus': False}

Default configuration for the column listboxes.

__init__(master, columns, column_weights=None, cnf={}, **kw)[source]

Construct a new multi-column listbox widget.

Parameters
  • master – The widget that should contain the new multi-column listbox.

  • columns – Specifies what columns should be included in the new multi-column listbox. If columns is an integer, the it is the number of columns to include. If it is a list, then its length indicates the number of columns to include; and each element of the list will be used as a label for the corresponding column.

  • kw (cnf,) –

    Configuration parameters for this widget. Use label_* to configure all labels; and listbox_* to configure all listboxes. E.g.:

    >>> mlb = MultiListbox(master, 5, label_foreground='red')
    

property column_names

A tuple containing the names of the columns used by this multi-column listbox.

property column_labels

A tuple containing the Tkinter.Label widgets used to display the label of each column. If this multi-column listbox was created without labels, then this will be an empty tuple. These widgets will all be augmented with a column_index attribute, which can be used to determine which column they correspond to. This can be convenient, e.g., when defining callbacks for bound events.

property listboxes

A tuple containing the Tkinter.Listbox widgets used to display individual columns. These widgets will all be augmented with a column_index attribute, which can be used to determine which column they correspond to. This can be convenient, e.g., when defining callbacks for bound events.

select(index=None, delta=None, see=True)[source]

Set the selected row. If index is specified, then select row index. Otherwise, if delta is specified, then move the current selection by delta (negative numbers for up, positive numbers for down). This will not move the selection past the top or the bottom of the list.

Parameters

see – If true, then call self.see() with the newly selected index, to ensure that it is visible.

configure(cnf={}, **kw)[source]

Configure this widget. Use label_* to configure all labels; and listbox_* to configure all listboxes. E.g.:

>>> mlb = MultiListbox(master, 5)
>>> mlb.configure(label_foreground='red')
>>> mlb.configure(listbox_foreground='red')
rowconfigure(row_index, cnf={}, **kw)[source]

Configure all table cells in the given row. Valid keyword arguments are: background, bg, foreground, fg, selectbackground, selectforeground.

columnconfigure(col_index, cnf={}, **kw)[source]

Configure all table cells in the given column. Valid keyword arguments are: background, bg, foreground, fg, selectbackground, selectforeground.

itemconfigure(row_index, col_index, cnf=None, **kw)[source]

Configure the table cell at the given row and column. Valid keyword arguments are: background, bg, foreground, fg, selectbackground, selectforeground.

insert(index, *rows)[source]

Insert the given row or rows into the table, at the given index. Each row value should be a tuple of cell values, one for each column in the row. Index may be an integer or any of the special strings (such as 'end') accepted by Tkinter.Listbox.

get(first, last=None)[source]

Return the value(s) of the specified row(s). If last is not specified, then return a single row value; otherwise, return a list of row values. Each row value is a tuple of cell values, one for each column in the row.

bbox(row, col)[source]

Return the bounding box for the given table cell, relative to this widget’s top-left corner. The bounding box is a tuple of integers (left, top, width, height).

hide_column(col_index)[source]

Hide the given column. The column’s state is still maintained: its values will still be returned by get(), and you must supply its values when calling insert(). It is safe to call this on a column that is already hidden.

See

show_column()

show_column(col_index)[source]

Display a column that has been hidden using hide_column(). It is safe to call this on a column that is not hidden.

bind_to_labels(sequence=None, func=None, add=None)[source]

Add a binding to each Tkinter.Label widget in this mult-column listbox that will call func in response to the event sequence.

Returns

A list of the identifiers of replaced binding functions (if any), allowing for their deletion (to prevent a memory leak).

bind_to_listboxes(sequence=None, func=None, add=None)[source]

Add a binding to each Tkinter.Listbox widget in this mult-column listbox that will call func in response to the event sequence.

Returns

A list of the identifiers of replaced binding functions (if any), allowing for their deletion (to prevent a memory leak).

bind_to_columns(sequence=None, func=None, add=None)[source]

Add a binding to each Tkinter.Label and Tkinter.Listbox widget in this mult-column listbox that will call func in response to the event sequence.

Returns

A list of the identifiers of replaced binding functions (if any), allowing for their deletion (to prevent a memory leak).

curselection(*args, **kwargs)[source]
selection_includes(*args, **kwargs)[source]
itemcget(*args, **kwargs)[source]
size(*args, **kwargs)[source]

Return a tuple of the number of column and rows in the grid.

index(*args, **kwargs)[source]
nearest(*args, **kwargs)[source]
activate(*args, **kwargs)[source]
delete(*args, **kwargs)[source]
scan_mark(*args, **kwargs)[source]
scan_dragto(*args, **kwargs)[source]
see(*args, **kwargs)[source]
selection_anchor(*args, **kwargs)[source]
selection_clear(*args, **kwargs)[source]

Clear the current X selection.

selection_set(*args, **kwargs)[source]
yview(*args, **kwargs)[source]
yview_moveto(*args, **kwargs)[source]
yview_scroll(*args, **kwargs)[source]
itemconfig(row_index, col_index, cnf=None, **kw)

Configure the table cell at the given row and column. Valid keyword arguments are: background, bg, foreground, fg, selectbackground, selectforeground.

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

Configure all table cells in the given row. Valid keyword arguments are: background, bg, foreground, fg, selectbackground, selectforeground.

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

Configure all table cells in the given column. Valid keyword arguments are: background, bg, foreground, fg, selectbackground, selectforeground.

select_anchor(*args, **kwargs)
select_clear(*args, **kwargs)

Clear the current X selection.

select_includes(*args, **kwargs)
select_set(*args, **kwargs)
class nltk.draw.table.Table[source]

Bases: object

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 list 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.

Variables
  • _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. Each element of _rows is a row value, i.e., a list of cell values, one for each column in the row.

__init__(master, column_names, rows=None, column_weights=None, scrollbar=True, click_to_sort=True, reprfunc=None, cnf={}, **kw)[source]

Construct a new Table widget.

Parameters
  • master (Tkinter.Widget) – The widget that should contain the new table.

  • column_names (list(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(list)) – A list of row values used to initialize 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.)

  • kw (cnf,) – Configuration parameters for this widget’s contained MultiListbox. See MultiListbox.__init__() for details.

pack(*args, **kwargs)[source]

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

grid(*args, **kwargs)[source]

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

focus()[source]

Direct (keyboard) input foxus to this widget.

bind(sequence=None, func=None, add=None)[source]

Add a binding to this table’s main frame that will call func in response to the event sequence.

rowconfigure(row_index, cnf={}, **kw)[source]
See

MultiListbox.rowconfigure()

columnconfigure(col_index, cnf={}, **kw)[source]
See

MultiListbox.columnconfigure()

itemconfigure(row_index, col_index, cnf=None, **kw)[source]
See

MultiListbox.itemconfigure()

bind_to_labels(sequence=None, func=None, add=None)[source]
See

MultiListbox.bind_to_labels()

bind_to_listboxes(sequence=None, func=None, add=None)[source]
See

MultiListbox.bind_to_listboxes()

bind_to_columns(sequence=None, func=None, add=None)[source]
See

MultiListbox.bind_to_columns()

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

MultiListbox.rowconfigure()

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

MultiListbox.columnconfigure()

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

MultiListbox.itemconfigure()

insert(row_index, rowvalue)[source]

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(rowvalues)[source]

Add new rows at the end of the table.

Parameters

rowvalues – A list of row values used to initialize the table. Each row value should be a tuple of cell values, one for each column in the row.

append(rowvalue)[source]

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.

clear()[source]

Delete all rows in this table.

property column_names

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

column_index(i)[source]

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(column_index)[source]
See

MultiListbox.hide_column()

show_column(column_index)[source]
See

MultiListbox.show_column()

selected_row()[source]

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(index=None, delta=None, see=True)[source]
See

MultiListbox.select()

sort_by(column_index, order='toggle')[source]

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 (column_index), then reverse the rows; otherwise sort in ascending order.

nltk.draw.table.demo()[source]