Skip to main content

This module is a Facade to simplify usage of the Gtk.TreeView with Gtk.ListStore model.

The module creates a list of Gtk.TreeViewColumn's and initialises it with the required column specifications and cell renderers.

The information as to the required columns and cell renderers is specified as a list of tuples in the calling module.

The following excerpts from my book "Programming Python with Gtk and SQLite" will illustrate.

4.21.2 The example data

Let's imagine we want to display some data selected from the Chinook example database (which will be discussed in Part 3). We want it to look as below.

The data will be presented in a separate file, employees.csv, to avoid any questions of database access at this stage. (Note: csv stands for "Comma Separated Variables".)

You will notice there are more data items in the file than are shown above. This is deliberate; the columns Address,City,State,Country etc. from the example database are present in the .csv file but are ignored for simplicity and to show that we aren't forced to display all the data.

Also note the fourth column "ReportsTo" is a numeric reference to the record for the named person's supervisor. This will come in handy to illustrate the TreeStore model later.

4.21.5 The ActiveList module

My ActiveList module was written to encapsulate the setting up of the tree model and treeview to avoid having to re-write this code for every new application, and repeat very similar code for each treeview column.

The ActiveList module (ActiveList.py) is provided under the MIT license.

If you import this module, you need only write a specification of the required columns, like this.

from ActiveList import TVColumn, ActiveList
# TreeModel column ID's
COL_SEQ = 0
COL_LAST = 1        # LastName
COL_FIRST = 2       # FirstName
COL_TITLE = 3       # Title
COL_BOSS = 4        # ReportsTo
COL_BORN = 5        # Birth Date
COL_HIRED = 6       # Hire Date
COL_SORT = 7        # Sort Key - not used
COL_KEY = 8         # Database Key - not used


#...or (easier to modify correctly)
COL_SEQ,\
COL_LAST,\
COL_FIRST,\
COL_TITLE,\
COL_BOSS,\
COL_BORN,\
COL_HIRED,\
COL_SORT,\
COL_KEY = range(9)

class TV(ActiveList):
    # The ActiveList defines the columns for the treeview
    _columns = [
        # We'll use column 0 of each row to specify a background colour for
        # the row. This is not compulsory but I found it a useful convention.
        # This column is not displayed.
        TVColumn(COL_SEQ, str)

        # The following columns are obtained from the data source
        # and displayed in the treeview.

        # column 1 (LastName)
        , TVColumn(COL_LAST, str, "LastName", 75, COL_SEQ, gtk.CellRendererText)
        # column 2 (FirstName)
        , TVColumn(COL_FIRST, str, "FirstName", 75, COL_SEQ, gtk.CellRendererText)
        # column 3 (Title)
        , TVColumn(COL_TITLE, str, "Title", 93, COL_SEQ, gtk.CellRendererText)
        # column 4 (Reports To)
        , TVColumn(COL_BOSS, str, "ReportsTo", 75, COL_SEQ, gtk.CellRendererText)
        # column 5 (BirthDate)
        , TVColumn(COL_BORN, str, "Born", 70, COL_SEQ, gtk.CellRendererText)
        # column 6 (HireDate)
        , TVColumn(COL_HIRED, str, "Hired", 70, COL_SEQ, gtk.CellRendererText)

        # The following column is used but not displayed
        # KEY - e.g. database key to identify a record for UPDATE etc
        , TVColumn(COL_KEY, int)
    ]

The parameters to TVColumn are

  • tree model column ID
  • column data type
  • column heading
  • column width (pixels)
  • model column to specify this column's background colour
  • cell renderer type
  • column justification (0 = left (default), 0.5 = centre, 1 = right)

ActiveList sets up the treeview which you supply with the columns and cell renderers you specify and returns the resulting "column_type_list" which can be used to create the treeview's model.

my_TV = TV(self.treeview)   # an instance of our descendant of ActiveList
                            # which defines the columns of the treeview
my_TV.model = gtk.ListStore(*my_TV.column_type_list)
self.treeview.set_model(my_TV.model)

If the column should be present in the model but not displayed in the view, only the first two parameters should be given. This is useful for example to keep a database key for every record in the model so we could update or delete the record if required.

You can specify any number of columns which can be used and/or displayed as you wish. You can use this for "future-proofing" e.g. specify columns for which the display code is not written yet.

The parameter "model column to specify this column's background colour" may need further explanation. The intention is to allow rows to have alternating background colours to help with reading the data, as in the section on "The example data". This could have been implemented within ActiveList, but I have just provided the framework for doing it yourself, to give users the choice.

I understand that some users will want this feature, but prefer it to be provided by their desktop theme. I might have gone that route but couldn't find a theme which worked.

Metadata

Release files for active-list-mc 0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for active-list-mc 0.6
File Size Uploaded Provenance
active-list-mc-0.6.tar.gz 5.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for active-list-mc 0.6
File Interpreter ABI Platform
active_list_mc-0.6-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / active-list-mc-0.6.tar.gz

Download URL active-list-mc-0.6.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0dbb1e439c3329efe0ba1e9f8b02a3d4d4910935b21cd76f8a8ca6e7d67b700b
BLAKE2b-256 checksum
How to use checksums
d42626be255f140cb92a3ddb2acc11149231ee3b20c60e42bab7e1ea6417c9bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.60.0 importlib-metadata/4.0.1 keyring/23.4.0 rfc3986/1.4.0 colorama/0.4.3 CPython/3.8.10

Release files / active_list_mc-0.6-py3-none-any.whl

Download URL active_list_mc-0.6-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88d9abf086b39dd26ebbafef4c4fbfb34c9099cf9efe6931f07dc45fa6d404bc
BLAKE2b-256 checksum
How to use checksums
27e9eccc75239a932f91dd464fdbf7b10054c300fc4ce67635fe0bc33491ad3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.60.0 importlib-metadata/4.0.1 keyring/23.4.0 rfc3986/1.4.0 colorama/0.4.3 CPython/3.8.10

Security for active-list-mc 0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page