跳转至

TableRow

Represents a row in a Tableview object

Source code in src/ttkbootstrap/widgets/tableview.py
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
class TableRow:
    """Represents a row in a Tableview object"""

    _cnt = 0

    def __init__(self, tableview: "Tableview", values: Sequence[Any]) -> None:
        """
        Parameters:

            tableview (Tableview):
                The Tableview widget that contains this row

            values (list[Any, ...]):
                A list of values to display in the row
        """
        self.view: ttk.Treeview = tableview.view
        self._values = list(values)
        self._iid = None
        self._sort = TableRow._cnt + 1
        self._table = tableview

        # increment cnt
        TableRow._cnt += 1

    @property
    def values(self) -> List[Any]:
        """The table row values"""
        return self._values

    @values.setter
    def values(self, values: Sequence[Any]) -> None:
        self._values = values
        self.refresh()

    @property
    def iid(self) -> str:
        """A unique record identifier"""
        return str(self._iid)

    def configure(self, opt: Optional[str] = None, **kwargs: Any) -> Union[Any, None]:
        """Configure the row. If opt is provided, the
        current value is returned, otherwise, sets the widget
        options specified in kwargs. See the documentation for
        `Tableview.insert_row` for configurable options.

        Parameters:

            opt (str):
                A configuration option to query.

            **kwargs { values, tags }:
                Optional keyword arguments used to configure the
                row.
        """
        if self._iid is None:
            self.build()

        if opt is not None:
            return self.view.item(self.iid, opt)
        elif 'values' in kwargs:
            values = kwargs.pop('values')
            self.values = values
        else:
            self.view.item(self.iid, **kwargs)

    def show(self, striped: bool = False) -> None:
        """Show the row in the data table view"""
        if self._iid is None:
            self.build()
        self.view.reattach(self.iid, "", END)

        # remove existing stripes
        tags = list(self.view.item(self.iid, "tags"))
        try:
            tags.remove("striped")
        except ValueError:
            pass

        # add stripes (if needed)
        if striped:
            tags.append("striped")
        self.view.item(self.iid, tags=tags)

    def delete(self) -> None:
        """Delete the row from the dataset"""
        if self.iid:
            self._table.iidmap.pop(self.iid)
            self._table.tablerows_visible.remove(self)
            self._table._tablerows.remove(self)
            self._table.load_table_data()
            self.view.delete(self.iid)

    def hide(self) -> None:
        """Remove the row from the data table view"""
        self.view.detach(self.iid)

    def refresh(self) -> None:
        """Syncs the tableview values with the object values"""
        if self._iid:
            self.view.item(self.iid, values=self.values)

    def build(self) -> None:
        """Create the row object in the `Treeview` and capture
        the resulting item id (iid).
        """
        if self._iid is None:
            # Use custom iid from specified field if configured
            if self._table._iid_field_index is not None:
                try:
                    custom_iid = str(self.values[self._table._iid_field_index])
                    self._iid = self.view.insert("", END, iid=custom_iid, values=self.values)
                except (IndexError, tk.TclError):
                    # Fall back to auto-generated iid if field doesn't exist or iid already exists
                    self._iid = self.view.insert("", END, values=self.values)
            else:
                self._iid = self.view.insert("", END, values=self.values)
            self._table.iidmap[self.iid] = self

iid property

A unique record identifier

values property writable

The table row values

__init__(tableview, values)

Parameters:

tableview (Tableview):
    The Tableview widget that contains this row

values (list[Any, ...]):
    A list of values to display in the row
Source code in src/ttkbootstrap/widgets/tableview.py
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def __init__(self, tableview: "Tableview", values: Sequence[Any]) -> None:
    """
    Parameters:

        tableview (Tableview):
            The Tableview widget that contains this row

        values (list[Any, ...]):
            A list of values to display in the row
    """
    self.view: ttk.Treeview = tableview.view
    self._values = list(values)
    self._iid = None
    self._sort = TableRow._cnt + 1
    self._table = tableview

    # increment cnt
    TableRow._cnt += 1

build()

Create the row object in the Treeview and capture the resulting item id (iid).

Source code in src/ttkbootstrap/widgets/tableview.py
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def build(self) -> None:
    """Create the row object in the `Treeview` and capture
    the resulting item id (iid).
    """
    if self._iid is None:
        # Use custom iid from specified field if configured
        if self._table._iid_field_index is not None:
            try:
                custom_iid = str(self.values[self._table._iid_field_index])
                self._iid = self.view.insert("", END, iid=custom_iid, values=self.values)
            except (IndexError, tk.TclError):
                # Fall back to auto-generated iid if field doesn't exist or iid already exists
                self._iid = self.view.insert("", END, values=self.values)
        else:
            self._iid = self.view.insert("", END, values=self.values)
        self._table.iidmap[self.iid] = self

configure(opt=None, **kwargs)

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

Parameters:

opt (str):
    A configuration option to query.

**kwargs { values, tags }:
    Optional keyword arguments used to configure the
    row.
Source code in src/ttkbootstrap/widgets/tableview.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
def configure(self, opt: Optional[str] = None, **kwargs: Any) -> Union[Any, None]:
    """Configure the row. If opt is provided, the
    current value is returned, otherwise, sets the widget
    options specified in kwargs. See the documentation for
    `Tableview.insert_row` for configurable options.

    Parameters:

        opt (str):
            A configuration option to query.

        **kwargs { values, tags }:
            Optional keyword arguments used to configure the
            row.
    """
    if self._iid is None:
        self.build()

    if opt is not None:
        return self.view.item(self.iid, opt)
    elif 'values' in kwargs:
        values = kwargs.pop('values')
        self.values = values
    else:
        self.view.item(self.iid, **kwargs)

delete()

Delete the row from the dataset

Source code in src/ttkbootstrap/widgets/tableview.py
405
406
407
408
409
410
411
412
def delete(self) -> None:
    """Delete the row from the dataset"""
    if self.iid:
        self._table.iidmap.pop(self.iid)
        self._table.tablerows_visible.remove(self)
        self._table._tablerows.remove(self)
        self._table.load_table_data()
        self.view.delete(self.iid)

hide()

Remove the row from the data table view

Source code in src/ttkbootstrap/widgets/tableview.py
414
415
416
def hide(self) -> None:
    """Remove the row from the data table view"""
    self.view.detach(self.iid)

refresh()

Syncs the tableview values with the object values

Source code in src/ttkbootstrap/widgets/tableview.py
418
419
420
421
def refresh(self) -> None:
    """Syncs the tableview values with the object values"""
    if self._iid:
        self.view.item(self.iid, values=self.values)

show(striped=False)

Show the row in the data table view

Source code in src/ttkbootstrap/widgets/tableview.py
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
def show(self, striped: bool = False) -> None:
    """Show the row in the data table view"""
    if self._iid is None:
        self.build()
    self.view.reattach(self.iid, "", END)

    # remove existing stripes
    tags = list(self.view.item(self.iid, "tags"))
    try:
        tags.remove("striped")
    except ValueError:
        pass

    # add stripes (if needed)
    if striped:
        tags.append("striped")
    self.view.item(self.iid, tags=tags)