From 46214e41743a8e00ff9cd1734eed200de0909091 Mon Sep 17 00:00:00 2001 From: nbro Date: Thu, 5 Nov 2026 16:21:16 +0100 Subject: [PATCH] Clarify Tktable in python -m tktable, document checking tkinter first --- CHANGELOG.md | 5 +++++ README.md | 3 ++- docs/history.md | 3 +++ docs/installation/index.md | 37 ++++++++++++++++++++++++++++++++++++- tktable/__main__.py | 38 +++++++++++++++++++++++++------------- 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6c27f0..ffc9439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,11 @@ and the project uses [Semantic Versioning](https://semver.org/). ### Changed +- `python -m tktable` calls Tktable "Tktable (Tcl/Tk extension)", so it's not confused with + pytktable, and links to the installation docs when tkinter doesn't work, instead of suggesting + a Debian/Ubuntu command on every OS. +- The installation docs explain how to check tkinter first, and how to fix it on macOS when + Homebrew's Tcl/Tk 9 broke a Python built against Tcl/Tk 8.6. - When Tktable cannot be loaded, the tests show the error of every try, and print a `Tktable load report` at the end, with Tcl's stack trace, what Tcl knows about Tktable and the files in `TKTABLE_LIBRARY`, to find the cause of the flaky failures on Windows. diff --git a/README.md b/README.md index 9a0fcc1..1cf5f7a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ A spreadsheet-like table widget for tkinter, wrapping the Tcl/Tk > [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). +The wrapper was originally written by [Guilherme Polo](https://github.com/gpip). For the story +behind pytktable and its binaries, see [this blog post](https://nbro.gitlab.io/python/2026/11/04/pytktable-a-spreadsheet-like-table-widget-for-tkinter-wrapping-the-tcl-tk-tktable-extension/). ![A window with a table of the 8 planets of the Solar System, made with pytktable](https://raw.githubusercontent.com/nbro/tktable/main/docs/images/planets.png) diff --git a/docs/history.md b/docs/history.md index 030f4ed..e6395aa 100644 --- a/docs/history.md +++ b/docs/history.md @@ -1,5 +1,8 @@ # History +Where Tcl/Tk, `tkinter` and Tktable come from. For how pytktable came to be, see +[this blog post](https://nbro.gitlab.io/python/2026/11/04/pytktable-a-spreadsheet-like-table-widget-for-tkinter-wrapping-the-tcl-tk-tktable-extension/). + ## Tcl/Tk [Tcl](https://www.tcl-lang.org/) (Tool Command Language) is a programming language designed by diff --git a/docs/installation/index.md b/docs/installation/index.md index a95154a..2411291 100644 --- a/docs/installation/index.md +++ b/docs/installation/index.md @@ -2,6 +2,18 @@ pytktable requires Python >= 3.11 with {py:mod}`tkinter`, Python's interface to Tcl/Tk. +(check-tkinter)= +## Check tkinter first + +pytktable can't install or fix tkinter, so first check that your Python has a working one: + +```sh +python -c "import tkinter; print(tkinter.TkVersion)" +``` + +It should print the version of Tcl/Tk your Python uses, e.g. `8.6` or `9.0`. If it fails instead, +fix tkinter first (see [Troubleshooting](#troubleshooting)), then install pytktable. + (prebuilt-tktable)= ## With prebuilt Tktable (recommended) @@ -9,7 +21,8 @@ pytktable requires Python >= 3.11 with {py:mod}`tkinter`, Python's interface to pip install "pytktable[binary]" ``` -This also installs [pytktable-binaries](https://github.com/nbro/pytktable-binaries), which contains +Keep the quotes: some shells, e.g. zsh (the default on macOS), would otherwise treat the brackets +as a file pattern. 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: @@ -75,6 +88,7 @@ If Tktable is installed in a directory Tcl does not search, set the environment `TKTABLE_LIBRARY` to the directory containing Tktable's `pkgIndex.tcl`. That directory is named after Tktable's version, e.g. `Tktable2.10` for Tktable 2.10. See your OS page for how to set it. +(troubleshooting)= ## Troubleshooting `_tkinter.TclError: can't find package Tktable` or `invalid command name "table"` @@ -87,6 +101,27 @@ after Tktable's version, e.g. `Tktable2.10` for Tktable 2.10. See your OS page f : tkinter is not installed for your Python. On Linux, install your distribution's tkinter package ([see above](#prebuilt-tktable)). +`ImportError: ... Library not loaded: .../libtk8.6.dylib` on macOS +: Your Python was compiled (e.g. by pyenv) against Homebrew's Tcl/Tk 8.6, which Homebrew has since + replaced with Tcl/Tk 9 in the `tcl-tk` formula. Python 3.14 and newer work with Tcl/Tk 9, so + reinstalling them is enough. Python 3.11 to 3.13 need Tcl/Tk 8.6, from Homebrew's `tcl-tk@8`; + with pyenv, e.g. for Python 3.11.9: + + ```sh + brew install tcl-tk@8 + ``` + + ```sh + pyenv uninstall -f 3.11.9 + ``` + + ```sh + PKG_CONFIG_PATH="$(brew --prefix tcl-tk@8)/lib/pkgconfig" pyenv install 3.11.9 + ``` + + Then [check tkinter](#check-tkinter) again, and recreate your virtual environments. The + python.org installers for macOS don't have this problem: they come with their own Tcl/Tk. + 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 vs 9.0). `python -m tktable` shows both. diff --git a/tktable/__main__.py b/tktable/__main__.py index ca05902..801052b 100644 --- a/tktable/__main__.py +++ b/tktable/__main__.py @@ -8,16 +8,28 @@ import platform import sys +#: Where to read how to install tkinter, Tcl/Tk and Tktable. +INSTALLATION_URL = "https://nbro.github.io/tktable/installation/" + + +def label(name: str) -> str: + """``name`` followed by a colon, padded so that the values line up.""" + return f"{name + ':':<28}" + + +#: Tktable is the Tcl/Tk extension, not this Python package (pytktable), so say it. +TKTABLE = label("Tktable (Tcl/Tk extension)") + def main() -> int: """Print diagnostic information and return the exit status.""" - print(f"Python: {sys.version.split()[0]} ({platform.platform()})") + print(f"{label('Python')}{sys.version.split()[0]} ({platform.platform()})") try: import tkinter # pylint: disable=import-outside-toplevel except ImportError as exc: - print(f"tkinter: NOT AVAILABLE ({exc})") - print("Install tkinter for your Python (e.g. `sudo apt install python3-tk`).") + print(f"{label('tkinter')}NOT AVAILABLE ({exc})") + print(f"Your Python has no working tkinter: see {INSTALLATION_URL}") return 1 # pylint: disable-next=import-outside-toplevel @@ -26,34 +38,34 @@ def main() -> int: try: interp = tkinter.Tcl() # Tcl only: no window, so no display needed. except tkinter.TclError as exc: - print(f"Tcl: NOT AVAILABLE ({exc})") + print(f"{label('Tcl')}NOT AVAILABLE ({exc})") return 1 - print(f"Tcl: {interp.call('info', 'patchlevel')}") - print(f"{TKTABLE_LIBRARY_ENV_VAR}: {os.environ.get(TKTABLE_LIBRARY_ENV_VAR, '(not set)')}") + print(f"{label('Tcl')}{interp.call('info', 'patchlevel')}") + print(f"{label(TKTABLE_LIBRARY_ENV_VAR)}{os.environ.get(TKTABLE_LIBRARY_ENV_VAR, '(not set)')}") versions = find_tktable(interp) # One folder per line, as plain text (Tcl 9 returns them as "path objects"). print("auto_path:") for folder in interp.tk.splitlist(interp.call("set", "auto_path")): print(f" {folder}") if not versions: - print("Tktable: NOT FOUND") - print("See https://nbro.github.io/tktable/installation/") + print(f"{TKTABLE}NOT FOUND") + print(f"See {INSTALLATION_URL}") return 1 - print(f"Tktable: found version(s) {', '.join(versions)}") + print(f"{TKTABLE}found version(s) {', '.join(versions)}") try: root = tkinter.Tk() except tkinter.TclError as exc: - print(f"Tk: cannot start ({exc}); skipped loading Tktable") + print(f"{label('Tk')}cannot start ({exc}); skipped loading Tktable") return 0 try: root.withdraw() - print(f"Tk: {root.call('info', 'patchlevel')}") - print(f"Tktable: loaded version {load_tktable(root)}") + print(f"{label('Tk')}{root.call('info', 'patchlevel')}") + print(f"{TKTABLE}loaded version {load_tktable(root)}") except tkinter.TclError as exc: - print(f"Tktable: FOUND BUT FAILED TO LOAD ({exc})") + print(f"{TKTABLE}FOUND BUT FAILED TO LOAD ({exc})") print("It may have been built for a different Tcl/Tk version than your Python uses.") return 1 finally: