How to install tkinter for python 3.8?

Solution 1:

If you installed python3.8 using apt (via ppa:deadsnakes/ppa), it can be installed using apt too, the name of library is python3.8-tk.

sudo apt install python3.8-tk

In my case, it solves the problem. For instance, now I can use matplotlib in python3.8 which requires tkinter.

Solution 2:

Recompile and reinstall python3.8 specifying path to folders with tcl, tk includes and libraries.

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev python-tk python3-tk tk-dev

cd ~/Downloads
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
tar -xvf Python-3.8.2.tgz

cd Python-3.8.2

Edit ./configure file the next way: replace ... for next lines:

  --with-tcltk-includes='-I/usr/include'
  --with-tcltk-libs='-L/usr/lib'

./configure
make -j2    # replace 2 by number of processor cores you have
sudo make install

$ python3.8
Python 3.8.2 (default, May 11 2020, 14:30:03) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

Python 3.8 installed through apt and pyenv on 16.04 does not include tkinter, as I think or there's a some bug that does not allow to import it. Only rebuilding helped. Previously I've used 20.04 with built in Python 3.8 version, which supports tkinter with only additional packages installing as it is for Python 3.5 on 16.04.

python3.8-tk installation as Ankur A Sharma said is also required. I've forget to mention it. But it is not sufficient for 16.04, at least in my case.

Additional requirement from OP's comment:

sudo ./configure --with-tcltk-includes='-I/usr/include -I/usr/include/tcl' --with-tcltk-libs='-L/usr/lib -ltcl -ltk' --enable-optimizations