Environment:
・MacOS Catalina 10.15.6
・pyenv 1.2.18
・Python 3.7.3
The first time I used the Tkinter package to create the GUI of my booklist APP, but there was an error occurred which showed that "No module named '_tkinter'.
So weird, because Tk/Tcl has long been an integral part of Python and the Tkinter package is a thin object-oriented layer on top of Tcl/Tk.
I have tried to uninstall python 3.8.1 ( I think maybe I installed the wrong version which can not compatible with the Tkinter) and install the Python 3.7.3, but still can not work. So it must be something else.
If you met the same problem with me, I recommend you read these articles here, they all give you the best way to solve it.
・ https://qiita.com/survivor7777777/items/5a8e23d30822437ae9f9
・ http://kidokorock.com/macos-catalina-pyenv-tkinter/
・https://stackoverflow.com/questions/60469202/unable-to-install-tkinter-with-pyenv-pythons-on-macos
All of the above solutions are derived from https://github.com/pyenv/pyenv/issues/1375
Now, let me briefly explain how I solved this problem.
Open your terminal:
・uninstall the python 3.7.3 ( pyenv uninstall 3.7.3)
・brew install tcl-tk
・brew info tcl-tk
・edit the python-build script file. The path of this file on my computer is /usr/local/Cellar/pyenv/1.2.13/plugins/python-build/bin/python-build
・open this file ( I use Atom), and search for this:
CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
then replace it with this :
CONFIGURE_OPTS --with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1
・install python 3.7.3 ( pyenv install 3.7.3)
・pyenv global 3.7.4
・Restart the terminal and input "python -m tkinter"
Then you'll see this.
Back to the editor and run the code again. There's nothing wrong with it!