Skip to content

TableColumn

Represents a column in a Tableview object.

Source code in src/ttkbootstrap/widgets/tableview.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
class TableColumn:
    """Represents a column in a Tableview object."""

    def __init__(
            self,
            tableview: "Tableview",
            cid: int,
            text: str,
            image: Any = "",
            command: Optional[Callable[[], None]] = None,
            anchor: Anchor = W,
            width: int = 200,
            minwidth: int = 20,
            stretch: bool = False,
    ) -> None:
        """
        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

    @property
    def headertext(self) -> str:
        """Return the text on the column header label.

        Returns:

            str: The header text.
        """
        return self._headertext

    @property
    def columnsort(self) -> int:
        """Return the sort direction used for this column.

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

        Returns:

            int: ``ASCENDING`` (0) or ``DESCENDING`` (1).
        """
        return self._sort

    @columnsort.setter
    def columnsort(self, value: int) -> None:
        self._sort = value

    @property
    def cid(self) -> str:
        """Return the unique column identifier.

        Returns:

            str: The column id.
        """
        return str(self._cid)

    @property
    def tableindex(self) -> Optional[int]:
        """Return the index of the column in the table configuration.

        Returns:

            int | None: The configured index of the column, or None.
        """
        cols = self.view.cget("columns")
        if cols is None:
            return
        try:
            return cols.index(self.cid)
        except IndexError:
            return

    @property
    def displayindex(self) -> Optional[int]:
        """Return the index of the column as displayed.

        Returns:

            int | None: The displayed index of the column, or None.
        """
        cols = self.view.cget("displaycolumns")
        if "#all" in cols:
            return self.tableindex
        else:
            return cols.index(self.cid)

    def configure(self, opt: Optional[str] = None, **kwargs: Any) -> Union[Any, None]:
        """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"]

    def show(self) -> None:
        """Make the column visible in the tableview"""
        displaycols = list(self.view.cget("displaycolumns"))
        if "#all" in displaycols:
            return
        if self.cid in displaycols:
            return
        columns = list(self.view.cget("columns"))
        index = columns.index(self.cid)
        displaycols.insert(index, self.cid)
        self.view.configure(displaycolumns=displaycols)

    def hide(self) -> None:
        """Hide the column in the tableview"""
        displaycols = list(self.view.cget("displaycolumns"))
        cols = list(self.view.cget("columns"))
        if "#all" in displaycols:
            displaycols = cols
        displaycols.remove(self.cid)
        self.view.configure(displaycolumns=displaycols)

    def delete(self) -> None:
        """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()

    def restore_settings(self) -> None:
        """Update the configuration based on stored settings"""
        self.view.column(self.cid, **self._settings_column)
        self.view.heading(self.cid, **self._settings_heading)

    def _capture_settings(self) -> None:
        """Update the stored settings for the column and heading.
        This is required because the settings are erased whenever
        the `columns` parameter is configured in the underlying
        Treeview widget."""
        self._settings_heading = self.view.heading(self.cid)
        self._settings_heading.pop("state")
        self._settings_column = self.view.column(self.cid)
        self._settings_column.pop("id")

cid property

Return the unique column identifier.

Returns:

str: The column id.

columnsort property writable

Return the sort direction used for this column.

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

Returns:

int: ``ASCENDING`` (0) or ``DESCENDING`` (1).

displayindex property

Return the index of the column as displayed.

Returns:

int | None: The displayed index of the column, or None.

headertext property

Return the text on the column header label.

Returns:

str: The header text.

tableindex property

Return the index of the column in the table configuration.

Returns:

int | None: The configured index of the column, or None.

__init__(tableview, cid, text, image='', command=None, 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.
Source code in src/ttkbootstrap/widgets/tableview.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def __init__(
        self,
        tableview: "Tableview",
        cid: int,
        text: str,
        image: Any = "",
        command: Optional[Callable[[], None]] = None,
        anchor: Anchor = W,
        width: int = 200,
        minwidth: int = 20,
        stretch: bool = False,
) -> None:
    """
    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(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.
Source code in src/ttkbootstrap/widgets/tableview.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
def configure(self, opt: Optional[str] = None, **kwargs: Any) -> Union[Any, None]:
    """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()

Remove the column from the tableview permanently.

Source code in src/ttkbootstrap/widgets/tableview.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def delete(self) -> None:
    """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()

Hide the column in the tableview

Source code in src/ttkbootstrap/widgets/tableview.py
261
262
263
264
265
266
267
268
def hide(self) -> None:
    """Hide the column in the tableview"""
    displaycols = list(self.view.cget("displaycolumns"))
    cols = list(self.view.cget("columns"))
    if "#all" in displaycols:
        displaycols = cols
    displaycols.remove(self.cid)
    self.view.configure(displaycolumns=displaycols)

restore_settings()

Update the configuration based on stored settings

Source code in src/ttkbootstrap/widgets/tableview.py
306
307
308
309
def restore_settings(self) -> None:
    """Update the configuration based on stored settings"""
    self.view.column(self.cid, **self._settings_column)
    self.view.heading(self.cid, **self._settings_heading)

show()

Make the column visible in the tableview

Source code in src/ttkbootstrap/widgets/tableview.py
249
250
251
252
253
254
255
256
257
258
259
def show(self) -> None:
    """Make the column visible in the tableview"""
    displaycols = list(self.view.cget("displaycolumns"))
    if "#all" in displaycols:
        return
    if self.cid in displaycols:
        return
    columns = list(self.view.cget("columns"))
    index = columns.index(self.cid)
    displaycols.insert(index, self.cid)
    self.view.configure(displaycolumns=displaycols)