Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ and the project uses [Semantic Versioning](https://semver.org/).

## [Unreleased]

## [0.3.0] - 2026-11-04

### Added

- `pip install "pytktable[binary]"` also installs Tktable, prebuilt for Linux, macOS and Windows
([pytktable-binaries](https://github.com/nbro/pytktable-binaries)). pytktable finds it
automatically, without installing Tktable or setting `TKTABLE_LIBRARY`.

### Changed

- The installation docs recommend `pip install "pytktable[binary]"`, and keep the instructions to
install Tktable yourself for platforms without prebuilt Tktable, or if you prefer your OS's.

## [0.2.0] - 2026-11-04

### Added
Expand Down Expand Up @@ -98,6 +111,7 @@ First release on PyPI. These changes were made with the help of
- `tktable/__init__.py` imports `ArrayVar` and `Table` lazily, so the diagnostic works
without tkinter (explained in the module docstring).

[Unreleased]: https://github.com/nbro/tktable/compare/0.2.0...HEAD
[Unreleased]: https://github.com/nbro/tktable/compare/0.3.0...HEAD
[0.3.0]: https://github.com/nbro/tktable/compare/0.2.0...0.3.0
[0.2.0]: https://github.com/nbro/tktable/compare/0.1.0...0.2.0
[0.1.0]: https://github.com/nbro/tktable/releases/tag/0.1.0
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ A spreadsheet-like table widget for tkinter, wrapping the Tcl/Tk

> ⚠️ **Important:** pytktable is only the Python wrapper. The table widget itself is
> [Tktable](https://wiki.tcl-lang.org/page/Tktable), an extension for Tcl/Tk written in C, which
> `pip install pytktable` does not install. Install Tktable first, following the
> [installation instructions](https://nbro.github.io/tktable/installation/) for your OS, then run
> `pip install pytktable` does not install. Install it with pytktable, prebuilt for Linux, macOS
> and Windows, with `pip install "pytktable[binary]"`, or install it yourself following the
> [installation instructions](https://nbro.github.io/tktable/installation/) for your OS. Then run
> `python -m tktable` to check that it works.

The wrapper was originally written by [Guilherme Polo](https://github.com/gpip).
Expand All @@ -38,13 +39,22 @@ System, from NASA's Planetary Fact Sheet (screenshot taken on Windows 11).

## Installation

With Tktable, prebuilt for Linux, macOS and Windows
([pytktable-binaries](https://github.com/nbro/pytktable-binaries)):

```sh
pip install "pytktable[binary]"
```

Without Tktable, if you install it yourself (see
[Installation](https://nbro.github.io/tktable/installation/)):

```sh
pip install pytktable
```

It is imported as `tktable` (`from tktable import Table`). It also needs the Tktable extension,
which `pip` does not install: see [Installation](https://nbro.github.io/tktable/installation/)
for how to install it on your OS, and run `python -m tktable` to check that it works.
It is imported as `tktable` (`from tktable import Table`). Run `python -m tktable` to check that
Tktable can be found and loaded.

## Documentation

Expand Down
7 changes: 4 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
A spreadsheet-like table widget for {py:mod}`tkinter`, wrapping the Tcl/Tk
[Tktable](https://wiki.tcl-lang.org/page/Tktable) extension.

```{admonition} Install Tktable separately
```{admonition} Install Tktable
:class: danger

pytktable is only the Python wrapper. The table widget itself is
[Tktable](https://wiki.tcl-lang.org/page/Tktable), an extension for Tcl/Tk written in C, which
`pip install pytktable` does not install. Install Tktable first, following the
[installation instructions](installation/index.md) for your OS, then run `python -m tktable` to
`pip install pytktable` does not install. Install it with pytktable, prebuilt for Linux, macOS and
Windows, with `pip install "pytktable[binary]"`, or install it yourself following the
[installation instructions](installation/index.md) for your OS. Then run `python -m tktable` to
check that it works.
```

Expand Down
62 changes: 48 additions & 14 deletions docs/installation/index.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
# Installation

Install the package with `pip`:
pytktable requires Python >= 3.11 with {py:mod}`tkinter`, Python's interface to Tcl/Tk.

(prebuilt-tktable)=
## With prebuilt Tktable (recommended)

```sh
pip install pytktable
pip install "pytktable[binary]"
```

This also installs [pytktable-binaries](https://github.com/nbro/pytktable-binaries), which contains
[Tktable](https://wiki.tcl-lang.org/page/Tktable), the Tcl/Tk extension that provides the table
widget, compiled for Tcl/Tk 8.6 and 9.0 for:

- Linux: x86_64 and ARM (aarch64), with glibc (most distributions) or musl (e.g. Alpine);
- macOS 11 and later: Intel and Apple silicon;
- Windows: x64, x86 and ARM.

pytktable finds it automatically, so you don't need to install Tktable or set `TKTABLE_LIBRARY`.
You only need tkinter, which the python.org installers for Windows and macOS include. On Linux,
install your distribution's tkinter package, e.g.:

```sh
sudo apt install python3-tk
```

It requires Python >= 3.11.
on Debian and Ubuntu, or:

```{admonition} Install Tktable separately
:class: danger
```sh
sudo dnf install python3-tkinter
```

on Fedora. Tktable also needs the X11 library (`libX11`), which every desktop Linux has.

(without-prebuilt-tktable)=
## Without prebuilt Tktable

`pip install pytktable` is not enough: it installs only the Python wrapper, not Tktable, the
Tcl/Tk extension that provides the widget. Install Tktable as below.
Install only the Python wrapper, and Tktable yourself, if:

- your platform is not in the list above (e.g. 32-bit ARM Linux, FreeBSD);
- you prefer the Tktable of your OS's package manager;
- the prebuilt Tktable doesn't work for you (please
[report it](https://github.com/nbro/pytktable-binaries/issues)).

```sh
pip install pytktable
```

This package is pure Python, but it also needs these, which `pip` does not install:
Then install these, which `pip install pytktable` does not install:

1. [Tcl](https://www.tcl-lang.org/) and [Tk](https://wiki.tcl-lang.org/page/Tk);
2. {py:mod}`tkinter`, Python's interface to Tk;
3. [Tktable](https://wiki.tcl-lang.org/page/Tktable), the Tk extension that provides the table
widget. It usually does not come with Tk.
1. [Tcl](https://www.tcl-lang.org/) and [Tk](https://wiki.tcl-lang.org/page/Tk), and
{py:mod}`tkinter`;
2. [Tktable](https://wiki.tcl-lang.org/page/Tktable). It usually does not come with Tk.

How to install them depends on your OS:

Expand All @@ -47,11 +78,14 @@ after Tktable's version, e.g. `Tktable2.10` for Tktable 2.10. See your OS page f
## Troubleshooting

`_tkinter.TclError: can't find package Tktable` or `invalid command name "table"`
: Tcl cannot find Tktable. Install it, or set [`TKTABLE_LIBRARY`](#tktable-library).
: Tcl cannot find Tktable. Install pytktable with [prebuilt Tktable](#prebuilt-tktable), or
[install Tktable](#without-prebuilt-tktable) and, if needed, set
[`TKTABLE_LIBRARY`](#tktable-library).
{py:class}`~tktable.table.Table` raises {py:class}`~tktable.table.CannotUseError` in this case.

`ModuleNotFoundError: No module named 'tkinter'`
: tkinter is not installed for your Python. See your OS page.
: tkinter is not installed for your Python. On Linux, install your distribution's tkinter
package ([see above](#prebuilt-tktable)).

Tktable is found but fails to load
: It was probably built for a different Tcl/Tk version than the one your Python uses (e.g. Tcl 8.6
Expand Down
3 changes: 3 additions & 0 deletions docs/installation/linux-macos.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Installation on Linux and macOS

These instructions are for installing Tktable yourself, with `pip install pytktable`. With
`pip install "pytktable[binary]"`, you don't need them: see {ref}`prebuilt-tktable`.

## Install tkinter and Tktable

On Debian and Ubuntu:
Expand Down
3 changes: 3 additions & 0 deletions docs/installation/windows.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Installation on Windows

These instructions are for installing Tktable yourself, with `pip install pytktable`. With
`pip install "pytktable[binary]"`, you don't need them: see {ref}`prebuilt-tktable`.

The python.org installers for Windows include tkinter, Tcl and Tk: Tcl 8.6 up to Python 3.13, and
Tcl 9.0 from Python 3.14.

Expand Down
24 changes: 23 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[project]
name = "pytktable"
version = "0.2.0"
version = "0.3.0"
description = "A spreadsheet-like table widget for tkinter, wrapping the Tcl/Tk Tktable extension."
readme = "README.md"
requires-python = ">=3.11"
Expand Down Expand Up @@ -40,6 +40,10 @@ classifiers = [
]
dependencies = []

[project.optional-dependencies]
# Prebuilt Tktable for Linux, macOS and Windows: pip install "pytktable[binary]".
binary = ["pytktable-binaries>=2.12.2"]

[project.urls]
documentation = "https://nbro.github.io/tktable/"
repository = "https://github.com/nbro/tktable"
Expand Down Expand Up @@ -93,6 +97,11 @@ warn_return_any = false
warn_unused_ignores = true
warn_redundant_casts = true

# pytktable-binaries is optional, so it may not be installed.
[[tool.mypy.overrides]]
module = ["tktable_binaries"]
ignore_missing_imports = true

[tool.pylint.main]
py-version = "3.11"

Expand Down
31 changes: 25 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import os
import time
from collections.abc import Iterator

import pytest
Expand All @@ -16,6 +17,14 @@
#: Set to 1 (e.g. in CI) to fail, instead of skip, the tests that need Tktable.
TKTABLE_REQUIRED = os.environ.get("TKTABLE_REQUIRED") == "1"

#: How many times to try to load Tktable, and how many seconds to wait between tries.
#: On GitHub's Windows runners, Tcl sometimes finds Tktable's pkgIndex.tcl but then can't read it
#: ("couldn't read file ... no such file or directory"), right after Tktable was installed: most
#: likely the antivirus is still scanning the new files. A later try reads it. Only with
#: TKTABLE_REQUIRED=1, so a run without Tktable isn't slowed down.
TKTABLE_LOAD_TRIES = 3 if TKTABLE_REQUIRED else 1
TKTABLE_LOAD_RETRY_SECONDS = 2


@pytest.fixture(scope="session")
def session_root() -> Iterator[tkinter.Tk | str]:
Expand All @@ -42,12 +51,22 @@ def session_tktable_error(session_root: tkinter.Tk | str) -> str:
"""
if isinstance(session_root, str):
return session_root
try:
load_tktable(session_root)
except tkinter.TclError as exc:
auto_path = session_root.tk.splitlist(session_root.tk.call("set", "auto_path"))
return f"{exc} (TKTABLE_LIBRARY={os.environ.get('TKTABLE_LIBRARY')}, auto_path={auto_path})"
return ""

error = ""
for tries in range(1, TKTABLE_LOAD_TRIES + 1):
try:
load_tktable(session_root)
return ""
except tkinter.TclError as exc:
error = str(exc)
if tries < TKTABLE_LOAD_TRIES:
time.sleep(TKTABLE_LOAD_RETRY_SECONDS)

auto_path = session_root.tk.splitlist(session_root.tk.call("set", "auto_path"))
folders = ", ".join(str(folder) for folder in auto_path)
library = os.environ.get("TKTABLE_LIBRARY")
details = f"tried {TKTABLE_LOAD_TRIES} times; TKTABLE_LIBRARY={library}; auto_path: {folders}"
return f"{error} ({details})"


@pytest.fixture
Expand Down
44 changes: 44 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# pylint: disable=missing-module-docstring, missing-function-docstring, missing-class-docstring, too-few-public-methods
from __future__ import annotations

import sys
import tkinter
import types
from pathlib import Path

import pytest

Expand Down Expand Up @@ -36,6 +39,12 @@ def __init__(self, versions: tuple[str, ...] = ()) -> None:
self.tk = FakeTk(versions)


@pytest.fixture(autouse=True)
def without_bundled_tktable(monkeypatch):
"""Run these tests as if pytktable-binaries were not installed, unless a test says otherwise."""
monkeypatch.setattr(utils, "_bundled_library", lambda: None)


def test_extend_auto_path_with_env(monkeypatch):
monkeypatch.setenv(utils.TKTABLE_LIBRARY_ENV_VAR, "/opt/Tktable2.10")
interp = FakeInterp()
Expand All @@ -50,6 +59,41 @@ def test_extend_auto_path_without_env(monkeypatch):
assert not interp.tk.calls


def test_extend_auto_path_with_bundled_tktable(monkeypatch):
monkeypatch.delenv(utils.TKTABLE_LIBRARY_ENV_VAR, raising=False)
monkeypatch.setattr(utils, "_bundled_library", lambda: "/site-packages/tktable_binaries")
interp = FakeInterp()
utils.extend_auto_path(interp) # type: ignore[arg-type]
assert interp.tk.calls == [("lappend", "auto_path", "/site-packages/tktable_binaries")]


def test_extend_auto_path_with_env_and_bundled_tktable(monkeypatch):
monkeypatch.setenv(utils.TKTABLE_LIBRARY_ENV_VAR, "/opt/Tktable2.10")
monkeypatch.setattr(utils, "_bundled_library", lambda: "/site-packages/tktable_binaries")
interp = FakeInterp()
utils.extend_auto_path(interp) # type: ignore[arg-type]
assert interp.tk.calls == [
("lappend", "auto_path", "/opt/Tktable2.10"),
("lappend", "auto_path", "/site-packages/tktable_binaries"),
]


def test_bundled_library_without_pytktable_binaries(monkeypatch):
monkeypatch.undo()
# None in sys.modules makes "import tktable_binaries" raise ImportError.
monkeypatch.setitem(sys.modules, "tktable_binaries", None)
assert utils._bundled_library() is None # pylint: disable=protected-access


def test_bundled_library_with_pytktable_binaries(monkeypatch):
monkeypatch.undo()
fake = types.ModuleType("tktable_binaries")
fake.library_dir = lambda: Path("/site-packages/tktable_binaries") # type: ignore[attr-defined]
monkeypatch.setitem(sys.modules, "tktable_binaries", fake)
expected = str(Path("/site-packages/tktable_binaries"))
assert utils._bundled_library() == expected # pylint: disable=protected-access


def test_find_tktable_scans_without_loading(monkeypatch):
monkeypatch.delenv(utils.TKTABLE_LIBRARY_ENV_VAR, raising=False)
interp = FakeInterp(versions=("2.10", "2.11"))
Expand Down
Loading
Loading