Repository navigation
Description
Bug report
Bug description:
I tried to add widgetPlus from Tklib and its dependency snit from Tcllib to my project, but executing root.tk.call("package", "require", "widgetPlus") after instantiating root = tkinter.Tk() failed with Tcl error:
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Users/oldium/AppData/Local/Programs/Python/Python312/lib/tcl8.6 C:/Users/oldium/AppData/Local/Programs/Python/lib/tcl8.6 C:/Users/oldium/AppData/Local/Programs/lib/tcl8.6 C:/Users/oldium/AppData/Local/Programs/Python/library C:/Users/oldium/AppData/Local/Programs/library C:/Users/oldium/AppData/Local/Programs/tcl8.6.13/library C:/Users/oldium/AppData/Local/tcl8.6.13/library
The real TCL installation path is C:/Users/oldium/AppData/Local/Programs/Python/Python312/tcl/tcl8.6, note tcl instead of lib.
Strangely, the same application worked when started as exe after PyInstaller was used. I tracked the problem down to the minimal test case (as used by Tcllib module snit):
import glob
import os
import sys
import tkinter as tk
from pathlib import Path
if __name__ == "__main__":
tcl = tk.Tcl()
interpreter = tcl.call("interp", "create")
tcl.call("interp", "delete", interpreter)
tcl.call("puts", "It worked!")I also found a workaround (I used glob to prevent fixing on a particular Tcl/Tk version), which works both in “normal” and venv environments:
# Fix Tcl/Tk folder
os.environ["TCL_LIBRARY"] = str(Path(glob.glob(os.path.join(sys.base_prefix, "tcl", "tcl*", "init.tcl"))[0]).parent)
os.environ["TK_LIBRARY"] = str(Path(glob.glob(os.path.join(sys.base_prefix, "tcl", "tk*", "pkgIndex.tcl"))[0]).parent)
os.environ["TIX_LIBRARY"] = str(Path(glob.glob(os.path.join(sys.base_prefix, "tcl", "tix*", "pkgIndex.tcl"))[0]).parent)
It looks like the Tcl/Tk installation works fine but needs additional configuration on the Python side. And please note that the workaround is usable for the Python's Tcl/Tk installation and does not consider existing Tcl/Tk installations and existing *_LIBRARY environmental variables.
CPython versions tested on:
3.11, 3.12
Operating systems tested on:
Windows
Activity
oldium commentedon Nov 5, 2023
This works in PyInstaller's generated
exebecause it sets the TCL_LIBRARY and TK_LIBRARY by itself as seen in Process Explorer:Maybe Python could do the same as PyInstaller does, i.e. set
TCL_LIBRARYandTK_LIBRARY(andTIX_LIBRARY?) if they are not already set?Mitigate Tkinter issue
FujiwaraChoki commentedon Nov 26, 2024
brew install tcl-tkfixed it for me.oldium commentedon Nov 26, 2024
On Windows? 🤔
Tcl/Tk is part of Python installer on Windows already.
Edit: You are talking about separate installation of Tcl/Tk, so you probably already have the environment variables set correctly, but I am talking about embedded version of Tcl/Tk installed by the Python installer on Windows, which resides in the Python installation directory.
FujiwaraChoki commentedon Nov 27, 2024
Yeah I'm on an M3 Mac, so not Windows. The issue is likely that there is a discrepancy between the Python and TCL/TK Versions. You need to re-install (re-build) Python everytime you install another version of TCL/TK.
I do not know the solution for windows.
oldium commentedon Nov 27, 2024
There is no discrepancy in versions. The Tcl/Tk runtime simply cannot find the installation path, because it expects it to be in
<Python Dir>/lib/tcl8.6(some auto-detection), but it is actually stored in<Python Dir>/tcl/tcl8.6. So the Tcl autodiscovery does not find it.I already mentioned possible solution/workaround in the issue description - set the environment variables
TCL_LIBRARY/TK_LIBRARY/TIX_LIBRARYby Python if they are not already set.FujiwaraChoki commentedon Nov 27, 2024
I know it's because of the paths.. The path had the wrong version in it which is why it wasn't working for me. Also I'm not on windows.