Skip to content

TableColumn

ttkbootstrap.tableview.TableColumn

Represents a column in a Tableview object

cid property readonly

A unique column identifier

columnsort property writable

Indicates how the column is to be sorted when the sorting method is invoked.

displayindex property readonly

The index of the column as it is displayed

headertext property readonly

The text on the header label

tableindex property readonly

The index of the column as it is in the table configuration.

__init__(self, tableview, cid, text, image='', command='', anchor='w', width=200, minwidth=20, stretch=False) special

Parameters:

Name Type Description Default
tableview Tableview

The parent tableview object.

required
cid str

The column id.

required
text str

The header text.

required
image PhotoImage

An image that is displayed to the left of the header text.

''
command Callable

A function called whenever the header button is clicked.

''
anchor str

The position of the header text within the header. One of "e", "w", "center".

'w'
width int

Specifies the width of the column in pixels.

200
minwidth int

Specifies the minimum width of the column in pixels.

20
stretch bool

Specifies whether or not the column width should be adjusted whenever the widget is resized or the user drags the column separator.

False
Source code in ttkbootstrap/tableview.py
def __init__(
        self,
        tableview,
        cid,
        text,
        image="",
        command="",
        anchor=W,
        width=200,
        minwidth=20,
        stretch=False,
):
    """
    Parameters:

        tableview (Tableview):
            The parent tableview object.

        cid (str):
            The column id.

        text (str):
            The header text.

        image (PhotoImage):
            An image that is displayed to the left of the header text.

        command (Callable):
            A function called whenever the header button is clicked.

        anchor (str):
            The position of the header text within the header. One
            of "e", "w", "center".

        width (int):
            Specifies the width of the column in pixels.

        minwidth (int):
            Specifies the minimum width of the column in pixels.

        stretch (bool):
            Specifies whether or not the column width should be
            adjusted whenever the widget is resized or the user
            drags the column separator.
    """
    self._table = tableview
    self._cid = cid
    self._headertext = text
    self._sort = ASCENDING
    self._settings_column = {}
    self._settings_heading = {}

    self.view: ttk.Treeview = tableview.view
    self.view.column(
        self._cid,
        width=width,
        minwidth=minwidth,
        stretch=stretch,
        anchor=anchor,
    )
    self.view.heading(
        self._cid,
        text=text,
        anchor=anchor,
        image=image,
        command=command,
    )
    self._capture_settings()
    self._table._cidmap[self._cid] = self

configure(self, opt=None, **kwargs)

Configure the column. If opt is provided, the current value is returned, otherwise, sets the widget options specified in kwargs. See the documentation for Tableview.insert_column for configurable options.

Parameters:

Name Type Description Default
opt str

A configuration option to query.

None
**kwargs Dict

Optional keyword arguments used to configure the column and headers.

{}
Source code in ttkbootstrap/tableview.py
def configure(self, opt=None, **kwargs):
    """Configure the column. If opt is provided, the
    current value is returned, otherwise, sets the widget
    options specified in kwargs. See the documentation for
    `Tableview.insert_column` for configurable options.

    Parameters:

        opt (str):
            A configuration option to query.

        **kwargs (Dict):
            Optional keyword arguments used to configure the
            column and headers.
    """
    # return queried options
    if opt is not None:
        if opt in ("anchor", "width", "minwidth", "stretch"):
            return self.view.column(self.cid, opt)
        elif opt in ("command", "text", "image"):
            return self.view.heading(self.cid, opt)
        else:
            return

    # configure column and heading
    for k, v in kwargs.items():
        if k in ("anchor", "width", "minwidth", "stretch"):
            self._settings_column[k] = v
        elif k in ("command", "text", "image"):
            self._settings_heading[k] = v
    self.view.column(self._cid, **self._settings_column)
    self.view.heading(self._cid, **self._settings_heading)
    if "text" in kwargs:
        self._headertext = kwargs["text"]

delete(self)

Remove the column from the tableview permanently.

Source code in ttkbootstrap/tableview.py
def delete(self):
    """Remove the column from the tableview permanently."""
    # update the tablerow columns
    index = self.tableindex
    if index is None:
        return

    for row in self._table.tablerows:
        row.values.pop(index)
        row.refresh()

    # actual columns
    cols = list(self.view.cget("columns"))
    cols.remove(self.cid)
    self._table.tablecolumns.remove(self)

    # visible columns
    dcols = list(self.view.cget("displaycolumns"))
    if "#all" in dcols:
        dcols = cols
    else:
        dcols.remove(self.cid)

    # remove cid mapping
    self._table.cidmap.pop(self._cid)

    # reconfigure the tableview column and displaycolumns
    self.view.configure(columns=cols, displaycolumns=dcols)

    # remove the internal object references
    for i, column in enumerate(self._table.tablecolumns):
        if column.cid == self.cid:
            self._table.tablecolumns.pop(i)
        else:
            column.restore_settings()

hide(self)

Hide the column in the tableview

Source code in ttkbootstrap/tableview.py
def hide(self):
    """Hide the column in the tableview"""
    displaycols = list(self.view.configure("displaycolumns"))
    cols = list(self.view.configure("columns"))
    if "#all" in displaycols:
        displaycols = cols
    displaycols.remove(self.cid)
    self.view.configure(displaycolumns=displaycols)

restore_settings(self)

Update the configuration based on stored settings

Source code in ttkbootstrap/tableview.py
def restore_settings(self):
    """Update the configuration based on stored settings"""
    self.view.column(self.cid, **self._settings_column)
    self.view.heading(self.cid, **self._settings_heading)

show(self)

Make the column visible in the tableview

Source code in ttkbootstrap/tableview.py
def show(self):
    """Make the column visible in the tableview"""
    displaycols = list(self.view.configure("displaycolumns"))
    if "#all" in displaycols:
        return
    if self.cid in displaycols:
        return
    columns = list(self.view.configure("columns"))
    index = columns.index(self.cid)
    displaycols.insert(index, self.cid)
    self.view.configure(displaycolumns=displaycols)