3

I'm getting an error while using tkinter. I have installed python using pyenv.

>>> import tkinter
>>> tkinter._test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/hrj/.pyenv/versions/3.8.16/lib/python3.8/tkinter/__init__.py", line 4557, in _test
    root = Tk()
  File "/Users/hrj/.pyenv/versions/3.8.16/lib/python3.8/tkinter/__init__.py", line 2272, in __init__
    self._loadtk()
  File "/Users/hrj/.pyenv/versions/3.8.16/lib/python3.8/tkinter/__init__.py", line 2288, in _loadtk
    raise RuntimeError("tk.h version (%s) doesn't match libtk.a version (%s)"
RuntimeError: tk.h version (8.5) doesn't match libtk.a version (8.6)

The error is similar to this question, but my tk.h version is lower. Any way to upgrade this?

tkinter._test() should display a test window but I get a error. In stackoverflow and GitHub people usually have a lower version of libtk.a but mine is the reverse. So those solutions won't work.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

2 Answers2

1

This worked in Python versions 3.7 to 3.11.

   brew update
   brew install tcl-tk
   echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.zshrc
   export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
   export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
   export PKG_CONFIG_PATH="/usr/local/opt/tcl-tk/lib/pkgconfig"
   brew install pyenv --head
   python --version >> py_ver
   sed -i '' "s/Python //g" py_ver
   export PY_VER="$( cat py_ver )"
   env \
   PATH="$(brew --prefix tcl-tk)/bin:$PATH" \
   LDFLAGS="-L$(brew --prefix tcl-tk)/lib" \
   CPPFLAGS="-I$(brew --prefix tcl-tk)/include" \
   PKG_CONFIG_PATH="$(brew --prefix tcl-tk)/lib/pkgconfig" \
   CFLAGS="-I$(brew --prefix tcl-tk)/include" \
   PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I$(brew --prefix tcl-tk)/include' --with-tcltk-libs='-L$(brew --prefix tcl-tk)/lib -ltcl8.6 -ltk8.6'" \
   pyenv install $PY_VER
   pyenv global $PY_VER

I have tried it on Github Actions.

Eftal Gezer
  • 191
  • 1
  • 8
0

The following is a workaround rather than a proper answer to this question. I would have preferred to post it as a comment but I do not have the required reputation.

The problem seems to be relatively wide-spread from what I gather. Someone in this discussion thread on github suggested that upgrading to Python >= 3.11 would solve the problem. I am not constrained to any particular Python version so this fixed the issue for me.

# pyenv uninstall 3.11.4
# (if applicable)

brew install tcl-tk
pyenv install 3.11.4