Tableview
ttkbootstrap.tableview.Tableview (Frame)
A class built on the ttk.Treeview
widget for arranging data in
rows and columns. The underlying Treeview object and its methods are
exposed in the Tableview.view
property.
A Tableview object contains various features such has striped rows, pagination, and autosized and autoaligned columns.
The pagination option is recommended when loading a lot of data as the table records are inserted on-demand. Table records are only created when requested to be in a page view. This allows the table to be loaded very quickly even with hundreds of thousands of records.
All table columns are sortable. Clicking a column header will toggle between sorting "ascending" and "descending".
Columns are configurable by passing a simple list of header names or by passing in a dictionary of column names with settings. You can use both as well, as in the example below, where a column header name is use for one column, and a dictionary of settings is used for another.
The object has a right-click menu on the header and the cells that allow you to configure various settings.
Examples:
Adding data with the constructor
import ttkbootstrap as ttk
from ttkbootstrap.tableview import Tableview
from ttkbootstrap.constants import *
app = ttk.Window()
colors = app.style.colors
coldata = [
{"text": "LicenseNumber", "stretch": False},
"CompanyName",
{"text": "UserCount", "stretch": False},
]
rowdata = [
('A123', 'IzzyCo', 12),
('A136', 'Kimdee Inc.', 45),
('A158', 'Farmadding Co.', 36)
]
dt = Tableview(
master=app,
coldata=coldata,
rowdata=rowdata,
paginated=True,
searchable=True,
bootstyle=PRIMARY,
stripecolor=(colors.light, None),
)
dt.pack(fill=BOTH, expand=YES, padx=10, pady=10)
app.mainloop()
Add data with methods
dt.insert_row('end', ['Marzale LLC', 26])
cidmap: Dict[str, ttkbootstrap.tableview.TableColumn]
property
readonly
A map of cid to tablecolumn object
iidmap: Dict[str, ttkbootstrap.tableview.TableRow]
property
readonly
A map of iid to tablerow object
is_filtered
property
readonly
Indicates whether the table is currently filtered
pagesize
property
writable
The number of records visible on a single page
searchcriteria
property
writable
The criteria used to filter the records when the search method is invoked
tablecolumns
property
readonly
A list of table column objects
tablecolumns_visible
property
readonly
A list of visible table column objects
tablerows
property
readonly
A list of all tablerow objects
tablerows_filtered
property
readonly
A list of filtered tablerow objects
tablerows_visible
property
readonly
A list of visible tablerow objects
__init__(self, master=None, bootstyle='default', coldata=[], rowdata=[], paginated=False, searchable=False, autofit=False, autoalign=True, stripecolor=None, pagesize=10, height=10, delimiter=',')
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
master |
Widget |
The parent widget. |
None |
bootstyle |
str |
A style keyword used to set the focus color of the entry and the background color of the date button. Available options include -> primary, secondary, success, info, warning, danger, dark, light. |
'default' |
coldata |
List[str | Dict] |
An iterable containing either the heading name or a
dictionary of column settings. Configurable settings
include >> text, image, command, anchor, width, minwidth,
maxwidth, stretch. Also see |
[] |
rowdata |
List |
An iterable of row data. The lenth of each row of data
must match the number of columns. Also see
|
[] |
paginated |
bool |
Specifies that the data is to be paginated. A pagination frame will be created below the table with controls that enable the user to page forward and backwards in the data set. |
False |
pagesize |
int |
When |
10 |
searchable |
bool |
If |
False |
autofit |
bool |
If |
False |
autoalign |
bool |
If |
True |
stripecolor |
Tuple[str, str] |
If provided, even numbered rows will be color using the
(background, foreground) specified. You may specify one
or the other by passing in None. For example,
|
None |
height |
int |
Specifies how many rows will appear in the table's viewport. If the number of records extends beyond the table height, the user may use the mousewheel or scrollbar to navigate the data. |
10 |
delimiter |
str |
The character to use as a delimiter when exporting data to CSV. |
',' |
Source code in ttkbootstrap/tableview.py
def __init__(
self,
master=None,
bootstyle=DEFAULT,
coldata=[],
rowdata=[],
paginated=False,
searchable=False,
autofit=False,
autoalign=True,
stripecolor=None,
pagesize=10,
height=10,
delimiter=",",
):
"""
Parameters:
master (Widget):
The parent widget.
bootstyle (str):
A style keyword used to set the focus color of the entry
and the background color of the date button. Available
options include -> primary, secondary, success, info,
warning, danger, dark, light.
coldata (List[str | Dict]):
An iterable containing either the heading name or a
dictionary of column settings. Configurable settings
include >> text, image, command, anchor, width, minwidth,
maxwidth, stretch. Also see `Tableview.insert_column`.
rowdata (List):
An iterable of row data. The lenth of each row of data
must match the number of columns. Also see
`Tableview.insert_row`.
paginated (bool):
Specifies that the data is to be paginated. A pagination
frame will be created below the table with controls that
enable the user to page forward and backwards in the
data set.
pagesize (int):
When `paginated=True`, this specifies the number of rows
to show per page.
searchable (bool):
If `True`, a searchbar will be created above the table.
Press the <Return> key to initiate a search. Searching
with an empty string will reset the search criteria, or
pressing the reset button to the right of the search
bar. Currently, the search method looks for any row
that contains the search text. The filtered results
are displayed in the table view.
autofit (bool):
If `True`, the table columns will be automatically sized
when loaded based on the records in the current view.
Also see `Tableview.autofit_columns`.
autoalign (bool):
If `True`, the column headers and data are automatically
aligned. Numbers and number headers are right-aligned
and all other data types are left-aligned. The auto
align method evaluates the first record in each column
to determine the data type for alignment. Also see
`Tableview.autoalign_columns`.
stripecolor (Tuple[str, str]):
If provided, even numbered rows will be color using the
(background, foreground) specified. You may specify one
or the other by passing in **None**. For example,
`stripecolor=('green', None)` will set the stripe
background as green, but the foreground will remain as
default. You may use standand color names, hexadecimal
color codes, or bootstyle color keywords. For example,
('light', '#222') will set the background to the "light"
themed ttkbootstrap color and the foreground to the
specified hexadecimal color. Also see
`Tableview.apply_table_stripes`.
height (int):
Specifies how many rows will appear in the table's viewport.
If the number of records extends beyond the table height,
the user may use the mousewheel or scrollbar to navigate
the data.
delimiter (str):
The character to use as a delimiter when exporting data
to CSV.
"""
super().__init__(master)
self._tablecols = []
self._tablerows = []
self._tablerows_filtered = []
self._viewdata = []
self._rowindex = tk.IntVar(value=0)
self._pageindex = tk.IntVar(value=1)
self._pagelimit = tk.IntVar(value=0)
self._height = height
self._pagesize = tk.IntVar(value=pagesize)
self._paginated = paginated
self._searchable = searchable
self._stripecolor = stripecolor
self._autofit = autofit
self._autoalign = autoalign
self._filtered = False
self._sorted = False
self._searchcriteria = tk.StringVar()
self._rightclickmenu_cell = None
self._delimiter = delimiter
self._iidmap = {} # maps iid to row object
self._cidmap = {} # maps cid to col object
self.view: ttk.Treeview = None
self._build_tableview_widget(coldata, rowdata, bootstyle)
align_column_center(self, event=None, cid=None)
Center align the column text. This can be triggered by
either an event, or by passing in the cid
, which is the index
of the column relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application event. |
None |
cid |
int |
A unique column identifier; typically the index of the column relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def align_column_center(self, event=None, cid=None):
"""Center align the column text. This can be triggered by
either an event, or by passing in the `cid`, which is the index
of the column relative to the original data set.
Parameters:
event (Event):
An application event.
cid (int):
A unique column identifier; typically the index of the
column relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
self.view.column(eo.column.cid, anchor=CENTER)
elif cid is not None:
self.view.column(cid, anchor=CENTER)
align_column_left(self, event=None, cid=None)
Left align the column text. This can be triggered by
either an event, or by passing in the cid
, which is the index
of the column relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application click event. |
None |
cid |
int |
A unique column identifier; typically the index of the column relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def align_column_left(self, event=None, cid=None):
"""Left align the column text. This can be triggered by
either an event, or by passing in the `cid`, which is the index
of the column relative to the original data set.
Parameters:
event (Event):
An application click event.
cid (int):
A unique column identifier; typically the index of the
column relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
self.view.column(eo.column.cid, anchor=W)
elif cid is not None:
self.view.column(cid, anchor=W)
align_column_right(self, event=None, cid=None)
Right align the column text. This can be triggered by
either an event, or by passing in the cid
, which is the index
of the column relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application event. |
None |
cid |
int |
A unique column identifier; typically the index of the column relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def align_column_right(self, event=None, cid=None):
"""Right align the column text. This can be triggered by
either an event, or by passing in the `cid`, which is the index
of the column relative to the original data set.
Parameters:
event (Event):
An application event.
cid (int):
A unique column identifier; typically the index of the
column relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
self.view.column(eo.column.cid, anchor=E)
elif cid is not None:
self.view.column(cid, anchor=E)
align_heading_center(self, event=None, cid=None)
Center align the heading text. This can be triggered by
either an event, or by passing in the cid
, which is the index
of the heading relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application event. |
None |
cid |
int |
A unique heading identifier; typically the index of the heading relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def align_heading_center(self, event=None, cid=None):
"""Center align the heading text. This can be triggered by
either an event, or by passing in the `cid`, which is the index
of the heading relative to the original data set.
Parameters:
event (Event):
An application event.
cid (int):
A unique heading identifier; typically the index of the
heading relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
self.view.heading(eo.column.cid, anchor=CENTER)
elif cid is not None:
self.view.heading(cid, anchor=CENTER)
align_heading_left(self, event=None, cid=None)
Left align the heading text. This can be triggered by
either an event, or by passing in the cid
, which is the index
of the heading relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application event. |
None |
cid |
int |
A unique heading identifier; typically the index of the heading relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def align_heading_left(self, event=None, cid=None):
"""Left align the heading text. This can be triggered by
either an event, or by passing in the `cid`, which is the index
of the heading relative to the original data set.
Parameters:
event (Event):
An application event.
cid (int):
A unique heading identifier; typically the index of the
heading relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
self.view.heading(eo.column.cid, anchor=W)
elif cid is not None:
self.view.heading(cid, anchor=W)
align_heading_right(self, event=None, cid=None)
Right align the heading text. This can be triggered by
either an event, or by passing in the cid
, which is the index
of the heading relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application event. |
None |
cid |
int |
A unique heading identifier; typically the index of the heading relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def align_heading_right(self, event=None, cid=None):
"""Right align the heading text. This can be triggered by
either an event, or by passing in the `cid`, which is the index
of the heading relative to the original data set.
Parameters:
event (Event):
An application event.
cid (int):
A unique heading identifier; typically the index of the
heading relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
self.view.heading(eo.column.cid, anchor=E)
elif cid is not None:
self.view.heading(cid, anchor=E)
apply_table_stripes(self, stripecolor)
Add stripes to even-numbered table rows as indicated by the
stripecolor
of (background, foreground). Either element may be
specified as None
, but both elements must be present.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stripecolor |
Tuple[str, str] |
A tuple of colors to apply to the table stripe. The tuple represents (background, foreground). |
required |
Source code in ttkbootstrap/tableview.py
def apply_table_stripes(self, stripecolor):
"""Add stripes to even-numbered table rows as indicated by the
`stripecolor` of (background, foreground). Either element may be
specified as `None`, but both elements must be present.
Parameters:
stripecolor (Tuple[str, str]):
A tuple of colors to apply to the table stripe. The
tuple represents (background, foreground).
"""
style: ttk.Style = ttk.Style.get_instance()
colors = style.colors
if len(stripecolor) == 2:
self._stripecolor = stripecolor
bg, fg = stripecolor
kw = {}
if bg is None:
kw["background"] = colors.active
else:
kw["background"] = bg
if fg is None:
kw["foreground"] = colors.inputfg
else:
kw["foreground"] = fg
self.view.tag_configure("striped", **kw)
autoalign_columns(self)
Align the columns and headers based on the data type of the values. Text is left-aligned; numbers are right-aligned. This method will have no effect if there is no data in the tables.
Source code in ttkbootstrap/tableview.py
def autoalign_columns(self):
"""Align the columns and headers based on the data type of the
values. Text is left-aligned; numbers are right-aligned. This
method will have no effect if there is no data in the tables."""
if len(self._tablerows) == 0:
return
values = self._tablerows[0]._values
for i, value in enumerate(values):
if str(value).isnumeric():
self.view.column(i, anchor=E)
self.view.heading(i, anchor=E)
else:
self.view.column(i, anchor=W)
self.view.heading(i, anchor=W)
autofit_columns(self)
Autofit all columns in the current view
Source code in ttkbootstrap/tableview.py
def autofit_columns(self):
"""Autofit all columns in the current view"""
f = font.nametofont("TkDefaultFont")
pad = utility.scale_size(self, 20)
col_widths = []
# measure header sizes
for col in self.tablecolumns:
width = f.measure(f"{col._headertext} {DOWNARROW}") + pad
col_widths.append(width)
for row in self.tablerows_visible:
values = row.values
for i, value in enumerate(values):
old_width = col_widths[i]
new_width = f.measure(str(value)) + pad
width = max(old_width, new_width)
col_widths[i] = width
for i, width in enumerate(col_widths):
self.view.column(i, width=width)
build_table_data(self, coldata, rowdata)
Insert the specified column and row data.
The coldata can be either a string column name or a dictionary
of column settings that are passed to the insert_column
method. You may use a mixture of string and dictionary in
the list of coldata.
!!!warning "Existing table data will be erased. This method will completely rebuild the underlying table with the new column and row data. Any existing data will be lost.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coldata |
List[Union[str, Dict]] |
An iterable of column names and/or settings. |
required |
rowdata |
List |
An iterable of row values. |
required |
Source code in ttkbootstrap/tableview.py
def build_table_data(self, coldata, rowdata):
"""Insert the specified column and row data.
The coldata can be either a string column name or a dictionary
of column settings that are passed to the `insert_column`
method. You may use a mixture of string and dictionary in
the list of coldata.
!!!warning "Existing table data will be erased.
This method will completely rebuild the underlying table
with the new column and row data. Any existing data will
be lost.
Parameters:
coldata (List[Union[str, Dict]]):
An iterable of column names and/or settings.
rowdata (List):
An iterable of row values.
"""
# destroy the existing data if existing
self.purge_table_data()
# build the table columns
for i, col in enumerate(coldata):
if isinstance(col, str):
# just a column name
self.insert_column(i, col)
else:
# a dictionary of column settings
self.insert_column(i, **col)
# build the table rows
for values in rowdata:
self.insert_row(values=values)
# load the table data
self.load_table_data()
# apply table formatting
if self._autofit:
self.autofit_columns()
if self._autoalign:
self.autoalign_columns()
if self._stripecolor is not None:
self.apply_table_stripes(self._stripecolor)
self.goto_first_page()
configure(self, cnf=None, **kwargs)
Configure the internal Treeview
widget. If cnf is provided,
value of the option is return. Otherwise the widget is
configured via kwargs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cnf |
Any |
An option to query. |
None |
**kwargs |
Dict |
Optional keyword arguments used to configure the internal Treeview widget. |
{} |
Returns:
Type | Description |
---|---|
Union[Any, None] |
The value of cnf or None. |
Source code in ttkbootstrap/tableview.py
def configure(self, cnf=None, **kwargs) -> Union[Any, None]:
"""Configure the internal `Treeview` widget. If cnf is provided,
value of the option is return. Otherwise the widget is
configured via kwargs.
Parameters:
cnf (Any):
An option to query.
**kwargs (Dict):
Optional keyword arguments used to configure the internal
Treeview widget.
Returns:
Union[Any, None]:
The value of cnf or None.
"""
try:
if "pagesize" in kwargs:
pagesize: int = kwargs.pop("pagesize")
self._pagesize.set(value=pagesize)
self.view.configure(cnf, **kwargs)
except:
super().configure(cnf, **kwargs)
delete_column(self, index=None, cid=None, visible=True)
Delete the specified column based on the column index or the unique cid.
Unless otherwise specified, the index refers to the column index as displayed in the tableview.
If cid is provided, the column associated with the cid is deleted regardless of whether it is in the visible data sets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int |
The numerical index of the column. |
None |
cid |
str |
A unique column indentifier. |
None |
visible |
bool |
Specifies that the index should refer to the visible columns. Otherwise, if False, the original column position is used. |
True |
Source code in ttkbootstrap/tableview.py
def delete_column(self, index=None, cid=None, visible=True):
"""Delete the specified column based on the column index or the
unique cid.
Unless otherwise specified, the index refers to the column index
as displayed in the tableview.
If cid is provided, the column associated with the cid is deleted
regardless of whether it is in the visible data sets.
Parameters:
index (int):
The numerical index of the column.
cid (str):
A unique column indentifier.
visible (bool):
Specifies that the index should refer to the visible
columns. Otherwise, if False, the original column
position is used.
"""
if cid is not None:
column: TableColumn = self.cidmap(int(cid))
column.delete()
elif index is not None and visible:
self.tablecolumns_visible[int(index)].delete()
elif index is None and not visible:
self.tablecolumns[int(index)].delete()
delete_columns(self, indices=None, cids=None, visible=True)
Delete columns specified by indices or cids.
Unless specified otherwise, the index refers to the position of the columns in the table from left to right starting with index 0.
!!!Warning "Use this method with caution!
This method may or may not suffer performance issues.
Internally, this method calls the delete_column
method
on each column specified in the list. The delete_column
method deletes the related column from each record in
the table data. So, if there are a lot of records this
could be problematic. It may be more beneficial to use
the build_table_data
if you plan on changing the
structure of the table dramatically.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indices |
List[int] |
A list of column indices to delete from the table. |
None |
cids |
List[str] |
A list of unique column identifiers to delete from the table. |
None |
visible |
bool |
If True, the index refers to the visible position of the column in the stable, from left to right starting at index 0. |
True |
Source code in ttkbootstrap/tableview.py
def delete_columns(self, indices=None, cids=None, visible=True):
"""Delete columns specified by indices or cids.
Unless specified otherwise, the index refers to the position
of the columns in the table from left to right starting with
index 0.
!!!Warning "Use this method with caution!
This method may or may not suffer performance issues.
Internally, this method calls the `delete_column` method
on each column specified in the list. The `delete_column`
method deletes the related column from each record in
the table data. So, if there are a lot of records this
could be problematic. It may be more beneficial to use
the `build_table_data` if you plan on changing the
structure of the table dramatically.
Parameters:
indices (List[int]):
A list of column indices to delete from the table.
cids (List[str]):
A list of unique column identifiers to delete from the
table.
visible (bool):
If True, the index refers to the visible position of the
column in the stable, from left to right starting at
index 0.
"""
if cids is not None:
for cid in cids:
self.delete_column(cid=cid)
elif indices is not None:
for index in indices:
self.delete_column(index=index, visible=visible)
delete_row(self, index=None, iid=None, visible=True)
Delete a record from the data set.
Unless specified otherwise, the index refers to the record position within the visible data set from top to bottom starting with index 0.
If iid is provided, the record associated with the cid is deleted regardless of whether it is in the visible data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int |
The numerical index of the record within the data set. |
None |
iid |
str |
A unique record identifier. |
None |
visible |
bool |
Indicates that the record index is relative to the current records in view, otherwise, the original data set index is used if False. |
True |
Source code in ttkbootstrap/tableview.py
def delete_row(self, index=None, iid=None, visible=True):
"""Delete a record from the data set.
Unless specified otherwise, the index refers to the record
position within the visible data set from top to bottom
starting with index 0.
If iid is provided, the record associated with the cid is deleted
regardless of whether it is in the visible data set.
Parameters:
index (int):
The numerical index of the record within the data set.
iid (str):
A unique record identifier.
visible (bool):
Indicates that the record index is relative to the current
records in view, otherwise, the original data set index is
used if False.
"""
# delete from iid
if iid is not None:
record: TableRow = self.iidmap.get(iid)
record.delete()
elif index is not None:
# visible index
if visible:
record = self.tablerows_visible[index]
record.delete()
# original index
else:
for record in self.tablerows:
if record._sort == index:
record.delete()
delete_rows(self, indices=None, iids=None, visible=True)
Delete rows specified by indices or iids.
If both indices and iids are None, then all records in the table will be deleted.
Source code in ttkbootstrap/tableview.py
def delete_rows(self, indices=None, iids=None, visible=True):
"""Delete rows specified by indices or iids.
If both indices and iids are None, then all records in the
table will be deleted.
"""
# remove records by iid
if iids is not None:
for iid in iids:
self.delete_row(iid=iid)
# remove records by index
elif indices is not None:
for index in indices:
self.delete_row(index=index, visible=visible)
# remove ALL records
else:
self._tablerows.clear()
self._tablerows_filtered.clear()
self._viewdata.clear()
self._iidmap.clear()
records = self.view.get_children()
self.view.delete(*records)
# route to new page if no records visible
if len(self._viewdata) == 0:
self.goto_page()
export_all_records(self)
Export all records to a csv file
Source code in ttkbootstrap/tableview.py
def export_all_records(self):
"""Export all records to a csv file"""
headers = [col.headertext for col in self.tablecolumns]
records = [row.values for row in self.tablerows]
self.save_data_to_csv(headers, records, self._delimiter)
export_current_page(self)
Export records on current page to csv file
Source code in ttkbootstrap/tableview.py
def export_current_page(self):
"""Export records on current page to csv file"""
headers = [col.headertext for col in self.tablecolumns]
records = [row.values for row in self.tablerows_visible]
self.save_data_to_csv(headers, records, self._delimiter)
export_current_selection(self)
Export rows currently selected to csv file
Source code in ttkbootstrap/tableview.py
def export_current_selection(self):
"""Export rows currently selected to csv file"""
headers = [col.headertext for col in self.tablecolumns]
selected = self.view.selection()
records = []
for iid in selected:
record: TableRow = self.iidmap.get(iid)
records.append(record.values)
self.save_data_to_csv(headers, records, self._delimiter)
export_records_in_filter(self)
Export rows currently filtered to csv file
Source code in ttkbootstrap/tableview.py
def export_records_in_filter(self):
"""Export rows currently filtered to csv file"""
headers = [col.headertext for col in self.tablecolumns]
if not self.is_filtered:
return
records = [row.values for row in self.tablerows_filtered]
self.save_data_to_csv(headers, records, self._delimiter)
fill_empty_columns(self, fillvalue='')
Fill empty columns with the fillvalue.
This method can be used to fill in missing values when a column column is inserted after data has already been inserted into the tableview.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fillvalue |
Any |
A value to insert into an empty column |
'' |
Source code in ttkbootstrap/tableview.py
def fill_empty_columns(self, fillvalue=""):
"""Fill empty columns with the fillvalue.
This method can be used to fill in missing values when a column
column is inserted after data has already been inserted into
the tableview.
Parameters:
fillvalue (Any):
A value to insert into an empty column
"""
rowcount = len(self._tablerows)
if rowcount == 0:
return
colcount = len(self._tablecols)
for row in self._tablerows:
var = colcount - len(row._values)
if var <= 0:
return
else:
for _ in range(var):
row._values.append(fillvalue)
row.configure(values=row._values)
filter_column_to_value(self, event=None, cid=None, value=None)
Hide all records except for records where the current column exactly matches the provided value. This method may be triggered by a window event or by specifying the column id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
A window click event. |
None |
cid |
int |
A unique column identifier; typically the numerical index of the column within the original dataset. |
None |
value |
Any |
The criteria used to filter the column. |
None |
Source code in ttkbootstrap/tableview.py
def filter_column_to_value(self, event=None, cid=None, value=None):
"""Hide all records except for records where the current
column exactly matches the provided value. This method may
be triggered by a window event or by specifying the column id.
Parameters:
event (Event):
A window click event.
cid (int):
A unique column identifier; typically the numerical
index of the column within the original dataset.
value (Any):
The criteria used to filter the column.
"""
if event is not None:
eo = self._get_event_objects(event)
index = eo.column.tableindex
value = value or eo.row.values[index]
elif cid is not None:
column: TableColumn = self.cidmap.get(cid)
index = column.tableindex
else:
return
self._filtered = True
self.tablerows_filtered.clear()
self.unload_table_data()
for row in self.tablerows:
if row.values[index] == value:
self.tablerows_filtered.append(row)
self._rowindex.set(0)
self.load_table_data()
filter_to_selected_rows(self)
Hide all records except for the selected rows
Source code in ttkbootstrap/tableview.py
def filter_to_selected_rows(self):
"""Hide all records except for the selected rows"""
criteria = self.view.selection()
if len(criteria) == 0:
return # nothing is selected
if self.is_filtered:
for row in self.tablerows_visible:
if row.iid not in criteria:
row.hide()
self.tablerows_filtered.remove(row)
else:
self._filtered = True
self.tablerows_filtered.clear()
for row in self.tablerows_visible:
if row.iid in criteria:
self.tablerows_filtered.append(row)
self._rowindex.set(0)
self.load_table_data()
get_column(self, index=None, visible=False, cid=None)
Returns the TableColumn
object from an index or a cid.
If index is specified, the column index refers to the index within the original, unless the visible flag is set, in which case the index is relative to the visible columns in view.
If cid is specified, the column associated with the cid is return regardless of whether it is visible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int |
The numerical index of the column. |
None |
visible |
bool |
Use the index of the visible columns as they appear in the table. |
False |
Returns:
Type | Description |
---|---|
Union[TableColumn, None] |
The table column object if found, otherwise None. |
Source code in ttkbootstrap/tableview.py
def get_column(
self, index=None, visible=False, cid=None
) -> TableColumn:
"""Returns the `TableColumn` object from an index or a cid.
If index is specified, the column index refers to the index
within the original, unless the visible flag is set, in which
case the index is relative to the visible columns in view.
If cid is specified, the column associated with the cid is
return regardless of whether it is visible.
Parameters:
index (int):
The numerical index of the column.
visible (bool):
Use the index of the visible columns as they appear
in the table.
Returns:
Union[TableColumn, None]:
The table column object if found, otherwise None.
"""
if cid is not None:
return self._cidmap.get(cid)
if not visible:
# original column index
try:
return self._tablecols[index]
except IndexError:
return None
else:
# visible column index
cols = self.view.cget("columns")
if len(cols) > 0:
cols = [int(x) for x in cols]
else:
cols = []
dcols = self.view.cget("displaycolumns")
if "#all" in dcols:
dcols = cols
else:
try:
x = int(dcols[index])
for c in self._tablecols:
if c.cid == x:
return c
except ValueError:
return None
get_columns(self)
Returns a list of all column objects. Same as using the
Tableview.tablecolumns
property.
Source code in ttkbootstrap/tableview.py
def get_columns(self) -> List[TableColumn]:
"""Returns a list of all column objects. Same as using the
`Tableview.tablecolumns` property."""
return self._tablecols
get_row(self, index=None, visible=False, filtered=False, iid=None)
Returns the TableRow
object from an index or the iid.
If an index is specified, the row index refers to the index within the original dataset. When choosing a subset of data, the visible data takes priority over filtered if both flags are set.
If an iid is specified, the object attached to that iid is returned regardless of whether or not it is visible or filtered.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int |
The numerical index of the column. |
None |
iid |
str |
A unique column identifier. |
None |
visible |
bool |
Use the index of the visible rows as they appear in the current table view. |
False |
filtered |
bool |
Use the index of the rows within the filtered data set. |
False |
Returns:
Type | Description |
---|---|
Union[TableRow, None] |
The table column object if found, otherwise None |
Source code in ttkbootstrap/tableview.py
def get_row(self, index=None, visible=False, filtered=False, iid=None) -> TableRow:
"""Returns the `TableRow` object from an index or the iid.
If an index is specified, the row index refers to the index
within the original dataset. When choosing a subset of data,
the visible data takes priority over filtered if both flags
are set.
If an iid is specified, the object attached to that iid is
returned regardless of whether or not it is visible or
filtered.
Parameters:
index (int):
The numerical index of the column.
iid (str):
A unique column identifier.
visible (bool):
Use the index of the visible rows as they appear
in the current table view.
filtered (bool):
Use the index of the rows within the filtered data
set.
Returns:
Union[TableRow, None]:
The table column object if found, otherwise None
"""
if iid is not None:
return self.iidmap.get(iid)
if visible:
try:
return self.tablerows_visible[index]
except IndexError:
return None
elif filtered:
try:
return self.tablerows_filtered[index]
except IndexError:
return None
else:
try:
return self.tablerows[index]
except IndexError:
return None
get_rows(self, visible=False, filtered=False, selected=False)
Return a list of TableRow objects.
Return a subset of rows based on optional flags. Only ONE flag can be used
at a time. If more than one flag is set to True
, then the first flag will
be used to return the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
visible |
bool |
If true, only records in the current view will be returned. |
False |
filtered |
bool |
If True, only rows in the filtered dataset will be returned. |
False |
selected |
bool |
If True, only rows that are currently selected will be returned. |
False |
Returns:
Type | Description |
---|---|
List[TableRow] |
A list of TableRow objects. |
Source code in ttkbootstrap/tableview.py
def get_rows(self, visible=False, filtered=False, selected=False) -> List[TableRow]:
"""Return a list of TableRow objects.
Return a subset of rows based on optional flags. Only ONE flag can be used
at a time. If more than one flag is set to `True`, then the first flag will
be used to return the data.
Parameters:
visible (bool):
If true, only records in the current view will be returned.
filtered (bool):
If True, only rows in the filtered dataset will be returned.
selected (bool):
If True, only rows that are currently selected will be returned.
Returns:
List[TableRow]:
A list of TableRow objects.
"""
if visible:
return self._viewdata
elif filtered:
return self._tablerows_filtered
elif selected:
return [row for row in self._viewdata if row.iid in self.view.selection()]
else:
return self._tablerows
goto_first_page(self)
Update table with first page of data
Source code in ttkbootstrap/tableview.py
def goto_first_page(self):
"""Update table with first page of data"""
self._rowindex.set(0)
self.load_table_data()
self._select_first_visible_item()
goto_last_page(self)
Update table with the last page of data
Source code in ttkbootstrap/tableview.py
def goto_last_page(self):
"""Update table with the last page of data"""
pagelimit = self._pagelimit.get() - 1
self._rowindex.set(self.pagesize * pagelimit)
self.load_table_data()
self._select_first_visible_item()
goto_next_page(self)
Update table with next page of data
Source code in ttkbootstrap/tableview.py
def goto_next_page(self):
"""Update table with next page of data"""
if self._pageindex.get() >= self._pagelimit.get():
return
rowindex = self._rowindex.get()
self._rowindex.set(rowindex + self.pagesize)
self.load_table_data()
self._select_first_visible_item()
goto_page(self, *_)
Go to a specific page indicated by the page entry widget.
Source code in ttkbootstrap/tableview.py
def goto_page(self, *_):
"""Go to a specific page indicated by the page entry widget."""
pagelimit = self._pagelimit.get()
pageindex = self._pageindex.get()
if pageindex > pagelimit:
pageindex = pagelimit
self._pageindex.set(pageindex)
elif pageindex <= 0:
pageindex = 1
self._pageindex.set(pageindex)
rowindex = (pageindex * self.pagesize) - self.pagesize
self._rowindex.set(rowindex)
self.load_table_data()
self._select_first_visible_item()
goto_prev_page(self)
Update table with prev page of data
Source code in ttkbootstrap/tableview.py
def goto_prev_page(self):
"""Update table with prev page of data"""
if self._pageindex.get() <= 1:
return
rowindex = self._rowindex.get()
self._rowindex.set(rowindex - self.pagesize)
self.load_table_data()
self._select_first_visible_item()
hide_selected_column(self, event=None, cid=None)
Detach the selected column from the tableview. This method may be triggered by a window event or by specifying the column id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
A window click event |
None |
cid |
int |
A unique column identifier; typically the numerical index of the column within the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def hide_selected_column(self, event=None, cid=None):
"""Detach the selected column from the tableview. This method
may be triggered by a window event or by specifying the column
id.
Parameters:
event (Event):
A window click event
cid (int):
A unique column identifier; typically the numerical
index of the column within the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
column = eo.column.hide()
elif cid is not None:
column: TableColumn = self.cidmap.get(cid)
column.hide()
hide_selected_rows(self)
Hide the currently selected rows
Source code in ttkbootstrap/tableview.py
def hide_selected_rows(self):
"""Hide the currently selected rows"""
selected = self.view.selection()
view_cnt = len(self._viewdata)
hide_cnt = len(selected)
self.view.detach(*selected)
tablerows = []
for row in self.tablerows_visible:
if row.iid in selected:
tablerows.append(row)
if not self.is_filtered:
self._filtered = True
self._tablerows_filtered = self.tablerows.copy()
for row in tablerows:
if self.is_filtered:
self.tablerows_filtered.remove(row)
if hide_cnt == view_cnt:
# assuming that if the count of the records on the page are
# selected for hiding, then need to go to the next page
# The call to `load_table_data` is duplicative, but currently
# this is the only way to get this to work until I've
# refactored this bit.
self.load_table_data()
self.goto_page()
else:
self.load_table_data()
insert_column(self, index, text='', image='', command='', anchor='w', width=200, minwidth=20, stretch=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
Union[int, str] |
A numerical index that specifieds where to insert the column. You may also use the string 'end' to insert the column in the right-most position. If the index exceeds the column count, it will be inserted at the right-most position. |
required |
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". |
'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 |
Returns:
Type | Description |
---|---|
TableColumn |
A table column object. |
Source code in ttkbootstrap/tableview.py
def insert_column(
self,
index,
text="",
image="",
command="",
anchor=W,
width=200,
minwidth=20,
stretch=False,
) -> TableColumn:
"""
Parameters:
index (Union[int, str]):
A numerical index that specifieds where to insert
the column. You may also use the string 'end' to
insert the column in the right-most position. If the
index exceeds the column count, it will be inserted
at the right-most position.
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.
Returns:
TableColumn:
A table column object.
"""
self.reset_table()
colcount = len(self.tablecolumns)
cid = colcount
if index == END:
index = -1
elif index > colcount - 1:
index = -1
# actual columns
cols = self.view.cget("columns")
if len(cols) > 0:
cols = [int(x) for x in cols]
cols.append(cid)
else:
cols = [cid]
# visible columns
dcols = self.view.cget("displaycolumns")
if "#all" in dcols:
dcols = cols
elif len(dcols) > 0:
dcols = [int(x) for x in dcols]
if index == -1:
dcols.append(cid)
else:
dcols.insert(index, cid)
else:
dcols = [cid]
self.view.configure(columns=cols, displaycolumns=dcols)
# configure new column
column = TableColumn(
tableview=self,
cid=cid,
text=text,
image=image,
command=command,
anchor=anchor,
width=width,
minwidth=minwidth,
stretch=stretch,
)
self._tablecols.append(column)
# must be called to show the header after initially creating it
# ad hoc, not sure why this should be the case;
self._column_sort_header_reset()
# update settings after they are erased when a column is
# inserted
for column in self._tablecols:
column.restore_settings()
return column
insert_row(self, index='end', values=[])
Insert a row into the tableview at index.
You must call Tableview.load_table_data()
to update the
current view. If the data is filtered, you will need to call
Tableview.load_table_data(clear_filters=True)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
Union[int, str] |
A numerical index that specifieds where to insert the record in the dataset. You may also use the string 'end' to append the record to the end of the data set. If the index exceeds the record count, it will be appended to the end of the dataset. |
'end' |
values |
Iterable |
An iterable of values to insert into the data set. The number of columns implied by the list of values must match the number of columns in the data set for the values to be visible. |
[] |
Returns:
Type | Description |
---|---|
TableRow |
A table row object. |
Source code in ttkbootstrap/tableview.py
def insert_row(self, index=END, values=[]) -> TableRow:
"""Insert a row into the tableview at index.
You must call `Tableview.load_table_data()` to update the
current view. If the data is filtered, you will need to call
`Tableview.load_table_data(clear_filters=True)`.
Parameters:
index (Union[int, str]):
A numerical index that specifieds where to insert
the record in the dataset. You may also use the string
'end' to append the record to the end of the data set.
If the index exceeds the record count, it will be
appended to the end of the dataset.
values (Iterable):
An iterable of values to insert into the data set.
The number of columns implied by the list of values
must match the number of columns in the data set for
the values to be visible.
Returns:
TableRow:
A table row object.
"""
rowcount = len(self._tablerows)
# validate the index
if len(values) == 0:
return
if index == END:
index = -1
elif index > rowcount - 1:
index = -1
record = TableRow(self, values)
if rowcount == 0 or index == -1:
self._tablerows.append(record)
else:
self._tablerows.insert(index, record)
return record
insert_rows(self, index, rowdata)
Insert row after index for each row in *row. If index does not exist then the records are appended to the end of the table. You can also use the string 'end' to append records at the end of the table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
Union[int, str] |
The location in the data set after where the records will be inserted. You may use a numerical index or the string 'end', which will append the records to the end of the data set. |
required |
rowdata |
List[Any, List] |
A list of row values to be inserted into the table. |
required |
Examples:
Tableview.insert_rows('end', ['one', 1], ['two', 2])
Source code in ttkbootstrap/tableview.py
def insert_rows(self, index, rowdata):
"""Insert row after index for each row in *row. If index does
not exist then the records are appended to the end of the table.
You can also use the string 'end' to append records at the end
of the table.
Parameters:
index (Union[int, str]):
The location in the data set after where the records
will be inserted. You may use a numerical index or
the string 'end', which will append the records to the
end of the data set.
rowdata (List[Any, List]):
A list of row values to be inserted into the table.
Examples:
```python
Tableview.insert_rows('end', ['one', 1], ['two', 2])
```
"""
if len(rowdata) == 0:
return
for values in reversed(rowdata):
self.insert_row(index, values)
load_table_data(self, clear_filters=False)
Load records into the tableview.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clear_filters |
bool |
Specifies that the table filters should be cleared before loading the data into the view. |
False |
Source code in ttkbootstrap/tableview.py
def load_table_data(self, clear_filters=False):
"""Load records into the tableview.
Parameters:
clear_filters (bool):
Specifies that the table filters should be cleared
before loading the data into the view.
"""
if len(self.tablerows) == 0:
return
if clear_filters:
self.reset_table()
self.unload_table_data()
if self._paginated:
page_start = self._rowindex.get()
page_end = self._rowindex.get() + self._pagesize.get()
else:
page_start = 0
page_end = len(self._tablerows)
if self._filtered:
rowdata = self._tablerows_filtered[page_start:page_end]
rowcount = len(self._tablerows_filtered)
else:
rowdata = self._tablerows[page_start:page_end]
rowcount = len(self._tablerows)
self._pagelimit.set(ceil(rowcount / self._pagesize.get()))
pageindex = ceil(page_end / self._pagesize.get())
pagelimit = self._pagelimit.get()
self._pageindex.set(min([pagelimit, pageindex]))
for i, row in enumerate(rowdata):
if self._stripecolor is not None and i % 2 == 0:
row.show(True)
else:
row.show(False)
self._viewdata.append(row)
move_column_left(self, event=None, cid=None)
Move column one position to the left. This can be triggered
by either an event, or by passing in the cid
, which is the
index of the column relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application click event. |
None |
cid |
int |
A unique column identifier; typically the index of the column relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def move_column_left(self, event=None, cid=None):
"""Move column one position to the left. This can be triggered
by either an event, or by passing in the `cid`, which is the
index of the column relative to the original data set.
Parameters:
event (Event):
An application click event.
cid (int):
A unique column identifier; typically the index of the
column relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
column = eo.column
elif cid is not None:
column = self.cidmap.get(cid)
else:
return
displaycols = [x.cid for x in self.tablecolumns_visible]
old_index = column.displayindex
if old_index == 0:
return
new_index = column.displayindex - 1
displaycols.insert(new_index, displaycols.pop(old_index))
self.view.configure(displaycolumns=displaycols)
move_column_right(self, event=None, cid=None)
Move column one position to the right. This can be triggered
by either an event, or by passing in the cid
, which is the
index of the column relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application click event. |
None |
cid |
int |
A unique column identifier; typically the index of the column relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def move_column_right(self, event=None, cid=None):
"""Move column one position to the right. This can be triggered
by either an event, or by passing in the `cid`, which is the
index of the column relative to the original data set.
Parameters:
event (Event):
An application click event.
cid (int):
A unique column identifier; typically the index of the
column relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
column = eo.column
elif cid is not None:
column = self.cidmap.get(cid)
else:
return
displaycols = [x.cid for x in self.tablecolumns_visible]
old_index = column.displayindex
if old_index == len(displaycols) - 1:
return
new_index = old_index + 1
displaycols.insert(new_index, displaycols.pop(old_index))
self.view.configure(displaycolumns=displaycols)
move_column_to_first(self, event=None, cid=None)
Move column to leftmost position. This can be triggered by
either an event, or by passing in the cid
, which is the index
of the column relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application click event. |
None |
cid |
int |
A unique column identifier; typically the index of the column relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def move_column_to_first(self, event=None, cid=None):
"""Move column to leftmost position. This can be triggered by
either an event, or by passing in the `cid`, which is the index
of the column relative to the original data set.
Parameters:
event (Event):
An application click event.
cid (int):
A unique column identifier; typically the index of the
column relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
column = eo.column
elif cid is not None:
column = self.cidmap.get(cid)
else:
return
displaycols = [x.cid for x in self.tablecolumns_visible]
old_index = column.displayindex
if old_index == 0:
return
displaycols.insert(0, displaycols.pop(old_index))
self.view.configure(displaycolumns=displaycols)
move_column_to_last(self, event=None, cid=None)
Move column to the rightmost position. This can be triggered
by either an event, or by passing in the cid
, which is the
index of the column relative to the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application click event. |
None |
cid |
int |
A unique column identifier; typically the index of the column relative to the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def move_column_to_last(self, event=None, cid=None):
"""Move column to the rightmost position. This can be triggered
by either an event, or by passing in the `cid`, which is the
index of the column relative to the original data set.
Parameters:
event (Event):
An application click event.
cid (int):
A unique column identifier; typically the index of the
column relative to the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
column = eo.column
elif cid is not None:
column = self.cidmap.get(cid)
else:
return
displaycols = [x.cid for x in self.tablecolumns_visible]
old_index = column.displayindex
if old_index == len(displaycols) - 1:
return
new_index = len(displaycols) - 1
displaycols.insert(new_index, displaycols.pop(old_index))
self.view.configure(displaycolumns=displaycols)
move_row_down(self)
Move the selected rows down one position in the dataset
Source code in ttkbootstrap/tableview.py
def move_row_down(self):
"""Move the selected rows down one position in the dataset"""
selected = self.view.selection()
if len(selected) == 0:
return
if self._filtered:
tablerows = self._tablerows_filtered
else:
tablerows = self._tablerows
for iid in selected:
row = self.iidmap.get(iid)
index = tablerows.index(row) + 1
tablerows.remove(row)
tablerows.insert(index, row)
if self._filtered:
self._tablerows_filtered = tablerows
else:
self._tablerows = tablerows
# refresh the table data
self.unload_table_data()
self.load_table_data()
move_selected_row_up(self)
Move the selected rows up one position in the dataset
Source code in ttkbootstrap/tableview.py
def move_selected_row_up(self):
"""Move the selected rows up one position in the dataset"""
selected = self.view.selection()
if len(selected) == 0:
return
if self.is_filtered:
tablerows = self._tablerows_filtered.copy()
else:
tablerows = self.tablerows.copy()
for iid in selected:
row = self.iidmap.get(iid)
index = tablerows.index(row) - 1
tablerows.remove(row)
tablerows.insert(index, row)
if self.is_filtered:
self._tablerows_filtered = tablerows
else:
self._tablerows = tablerows
# refresh the table data
self.unload_table_data()
self.load_table_data()
move_selected_rows_to_bottom(self)
Move the selected rows to the bottom of the dataset
Source code in ttkbootstrap/tableview.py
def move_selected_rows_to_bottom(self):
"""Move the selected rows to the bottom of the dataset"""
selected = self.view.selection()
if len(selected) == 0:
return
if self.is_filtered:
tablerows = self.tablerows_filtered.copy()
else:
tablerows = self.tablerows.copy()
for iid in selected:
row = self.iidmap.get(iid)
tablerows.remove(row)
tablerows.append(row)
if self.is_filtered:
self._tablerows_filtered = tablerows
else:
self._tablerows = tablerows
# refresh the table data
self.unload_table_data()
self.load_table_data()
move_selected_rows_to_top(self)
Move the selected rows to the top of the data set
Source code in ttkbootstrap/tableview.py
def move_selected_rows_to_top(self):
"""Move the selected rows to the top of the data set"""
selected = self.view.selection()
if len(selected) == 0:
return
if self.is_filtered:
tablerows = self.tablerows_filtered.copy()
else:
tablerows = self.tablerows.copy()
for i, iid in enumerate(selected):
row = self.iidmap.get(iid)
tablerows.remove(row)
tablerows.insert(i, row)
if self.is_filtered:
self._tablerows_filtered = tablerows
else:
self._tablerows = tablerows
# refresh the table data
self.unload_table_data()
self.load_table_data()
purge_table_data(self)
Erase all table and column data.
This method will completely destroy the table data structure. The table will need to be completely rebuilt after using this method.
Source code in ttkbootstrap/tableview.py
def purge_table_data(self):
"""Erase all table and column data.
This method will completely destroy the table data structure.
The table will need to be completely rebuilt after using this
method.
"""
self.delete_rows()
self.cidmap.clear()
self.tablecolumns.clear()
self.view.configure(columns=[], displaycolumns=[])
reset_column_filters(self)
Remove all column level filters; unhide all columns.
Source code in ttkbootstrap/tableview.py
def reset_column_filters(self):
"""Remove all column level filters; unhide all columns."""
cols = [col.cid for col in self.tablecolumns]
self.view.configure(displaycolumns=cols)
reset_column_sort(self)
Display all columns by original insert index
Source code in ttkbootstrap/tableview.py
def reset_column_sort(self):
"""Display all columns by original insert index"""
cols = sorted([col.cid for col in self.tablecolumns_visible], key=int)
self.view.configure(displaycolumns=cols)
reset_row_filters(self)
Remove all row level filters; unhide all rows.
Source code in ttkbootstrap/tableview.py
def reset_row_filters(self):
"""Remove all row level filters; unhide all rows."""
self._filtered = False
self.searchcriteria = ""
self.unload_table_data()
self.load_table_data()
reset_row_sort(self)
Display all table rows by original insert index
Source code in ttkbootstrap/tableview.py
def reset_row_sort(self):
"""Display all table rows by original insert index"""
...
reset_table(self)
Remove all table data filters and column sorts
Source code in ttkbootstrap/tableview.py
def reset_table(self):
"""Remove all table data filters and column sorts"""
self._filtered = False
self.searchcriteria = ""
try:
sortedrows = sorted(self.tablerows, key=lambda x: x._sort)
except IndexError:
self.fill_empty_columns()
sortedrows = sorted(self.tablerows, key=lambda x: x._sort)
self._tablerows = sortedrows
self.unload_table_data()
# reset the columns
self.reset_column_filters()
self.reset_column_sort()
self._column_sort_header_reset()
self.goto_first_page() # needed?
save_data_to_csv(self, headers, records, delimiter=',')
Save data records to a csv file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
headers |
List[str] |
A list of header labels. |
required |
records |
List[Tuple[...]] |
A list of table records. |
required |
delimiter |
str |
The character to use for delimiting the values. |
',' |
Source code in ttkbootstrap/tableview.py
def save_data_to_csv(self, headers, records, delimiter=","):
"""Save data records to a csv file.
Parameters:
headers (List[str]):
A list of header labels.
records (List[Tuple[...]]):
A list of table records.
delimiter (str):
The character to use for delimiting the values.
"""
from tkinter.filedialog import asksaveasfilename
import csv
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
initialfile = f"tabledata_{timestamp}.csv"
filetypes = [
("CSV UTF-8 (Comma delimited)", "*.csv"),
("All file types", "*.*"),
]
filename = asksaveasfilename(
confirmoverwrite=True,
filetypes=filetypes,
defaultextension="csv",
initialfile=initialfile,
)
if filename:
with open(filename, "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f, delimiter=delimiter)
writer.writerow(headers)
writer.writerows(records)
sort_column_data(self, event=None, cid=None, sort=None)
Sort the table rows by the specified column. This method may be trigged by an event or manually.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
A window event. |
None |
cid |
int |
A unique column identifier; typically the numerical index of the column relative to the original data set. |
None |
sort |
int |
Determines the sort direction. 0 = ASCENDING. 1 = DESCENDING. |
None |
Source code in ttkbootstrap/tableview.py
def sort_column_data(self, event=None, cid=None, sort=None):
"""Sort the table rows by the specified column. This method
may be trigged by an event or manually.
Parameters:
event (Event):
A window event.
cid (int):
A unique column identifier; typically the numerical
index of the column relative to the original data set.
sort (int):
Determines the sort direction. 0 = ASCENDING. 1 = DESCENDING.
"""
if event is not None:
eo = self._get_event_objects(event)
column = eo.column
index = column.tableindex
elif cid is not None:
column: TableColumn = self.cidmap.get(int(cid))
index = column.tableindex
else:
return
# update table data
if self.is_filtered:
tablerows = self.tablerows_filtered
else:
tablerows = self.tablerows
if sort is not None:
columnsort = sort
else:
columnsort = self.tablecolumns[index].columnsort
if columnsort == ASCENDING:
self._tablecols[index].columnsort = DESCENDING
else:
self._tablecols[index].columnsort = ASCENDING
try:
sortedrows = sorted(
tablerows, reverse=columnsort, key=lambda x: x.values[index]
)
except:
# when data is missing, or sometimes with numbers
# this is still not right, but it works most of the time
# fix sometime down the road when I have time
self.fill_empty_columns()
sortedrows = sorted(
tablerows, reverse=columnsort, key=lambda x: int(x.values[index])
)
if self.is_filtered:
self._tablerows_filtered = sortedrows
else:
self._tablerows = sortedrows
# update headers
self._column_sort_header_reset()
self._column_sort_header_update(column.cid)
self.unload_table_data()
self.load_table_data()
self._select_first_visible_item()
unhide_selected_column(self, event=None, cid=None)
Attach the selected column to the tableview. This method may be triggered by a window event or by specifying the column id. The column is reinserted at the index in the original data set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
Event |
An application click event |
None |
cid |
int |
A unique column identifier; typically the numerical index of the column within the original dataset. |
None |
Source code in ttkbootstrap/tableview.py
def unhide_selected_column(self, event=None, cid=None):
"""Attach the selected column to the tableview. This method
may be triggered by a window event or by specifying the column
id. The column is reinserted at the index in the original data
set.
Parameters:
event (Event):
An application click event
cid (int):
A unique column identifier; typically the numerical
index of the column within the original dataset.
"""
if event is not None:
eo = self._get_event_objects(event)
eo.column.show()
elif cid is not None:
column = self.cidmap.get(cid)
column.show()
unload_table_data(self)
Unload all data from the table
Source code in ttkbootstrap/tableview.py
def unload_table_data(self):
"""Unload all data from the table"""
for row in self.tablerows_visible:
row.hide()
self.tablerows_visible.clear()