0
0

More than 3 years have passed since last update.

How to solve the problem [ tkinter can not work at the environment python 3.7.3]

Last updated at Posted at 2020-07-25

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'.
スクリーンショット 2020-07-25 19.24.58.png
スクリーンショット 2020-07-25 19.18.33.png

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
スクリーンショット 2020-07-25 20.24.22.png

・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
スクリーンショット 2020-07-25 20.28.37.png

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

スクリーンショット 2020-07-25 19.58.07.png

Back to the editor and run the code again. There's nothing wrong with it!

スクリーンショット 2020-07-25 20.35.17.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0