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.
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.