Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install tkinter in python3.6 on Ubuntu

+--------+-----------------------------------+
|   OS   |           Ubuntu 12.04            |
+--------+-----------------------------------+
| Python | 2.7, 3.2 and source installed 3.6 |
+--------+-----------------------------------+

Since there are 2 versions of Python 3, anything installed from the repository doesn't work for Python 3.6. The latest version of Python in the repositories is 3.2, so I need source installs or through pip3.6.

After starting python3.6 I tried to import tkinter, which gave the following error. Even though help('modules') returned a list of modules which included tkinter.

 import tkinter
 ModuleNotFoundError: No module named '_tkinter'

I tried doing the same in python3.2 and there were no errors. tkinter._tkinter gave the location of tkinter library for python3.2

I cd'd into the python3.6 directory which has all library files and indeed it was missing the tkinter.so object file.

How do I fix the error?

I would like to get tkinter/tkagg working as it seems all the modules/ package are already installed.

After googling some more, I found I need to build python3.6 again, but this time with Tcl/Tk options while running configure. I'd rather not. It takes 1hr approx to install python3.6 from scratch.

Is there some other way where I can tell python3.6 where Tcl/Tk is located?

The problem isn't telling python where tcl/tk is. After messing around with python3.6's source code, and then comparing python3.6 with python3.2, I found out that tkinter calls _tkinter which isn't a python file, it's an .so(shared object) file that python builds during installation via setup.py that uses gcc, that somehow may involve distutils.

The new and more appropriate question is how do I build, _tkinter.cpython-36m-i386-linux-gnu.so from tcl/tk?

Note : I do have tcl/tk installed, which I have confirmed using tclsh and wish.

like image 978
lapin Avatar asked Mar 06 '17 10:03

lapin


People also ask

How do I install tkinter on Ubuntu?

Instructions for Ubuntu Install Tkinter: apt-get install python-tk (restart after) Install setuptools: sudo apt-get install python-setuptools. Install suds: sudo easy_install suds. Install matplotlib: sudo apt-get install python-matplotlib.

Does Python 3.6 have tkinter?

25.1. tkinter — Python interface to Tcl/Tk — Python 3.6.

Does Python 3.8 have tkinter?

Tkinter works just fine on Python 3.8.


3 Answers

run from terminal:

sudo apt-get install python3.6-tk

or just reinstall completly:

sudo apt-get install python3.6
like image 113
Enchant97 Avatar answered Oct 12 '22 10:10

Enchant97


Python version 3.6.4 (Ubuntu 18.04 LTS)

sudo add-apt-repository main

sudo apt-get install python3-tk
like image 37
macm Avatar answered Oct 12 '22 10:10

macm


I faced a similar problem to yours, I am giving the details of it and how I solved it.

On Ubuntu 16.04 LTS, I have Python 3.5.2 and Python 2.7.12 but I would like to experiment Python3.6 (for various reasons like this one, for example). So I relied on this post:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6

When I tried to run a module using tkinter, I got this error message:

Traceback (most recent call last):
  File "/usr/lib/python3.6/tkinter/__init__.py", line 37, in <module>
    import _tkinter
ModuleNotFoundError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "bill.py", line 3, in <module>
    from tkinter import Canvas, Label, Tk, StringVar, Button, LEFT
  File "/usr/lib/python3.6/tkinter/__init__.py", line 39, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package

I tried to install tkinter as the message above asks:

sudo apt-get install python3-tk
[sudo] password for begueradj: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-tk is already the newest version (3.5.1-1).
0 upgraded, 0 newly installed, 0 to remove and 8 not upgraded.

Obviously, I still can not use tkinter for Python 3.6. How to fix this problem?

My first blind attempt did not work:

sudo apt-get install python36-tk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python36-tk

The second one works:

sudo apt-get install python3.6-tk
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  tix python3.6-tk-dbg
The following NEW packages will be installed:
  python3.6-tk
0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.
Need to get 74.6 kB of archives.
After this operation, 165 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.6-tk amd64 3.6.5-1+xenial1 [74.6 kB]
Fetched 74.6 kB in 0s (301 kB/s)        
Selecting previously unselected package python3.6-tk:amd64.
(Reading database ... 324106 files and directories currently installed.)
Preparing to unpack .../python3.6-tk_3.6.5-1+xenial1_amd64.deb ...
Unpacking python3.6-tk:amd64 (3.6.5-1+xenial1) ...
Setting up python3.6-tk:amd64 (3.6.5-1+xenial1) ...

And that solved my problem:

~/python3.6
Python 3.6.5 (default, Mar 29 2018, 03:28:50) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 
like image 39
Billal Begueradj Avatar answered Oct 12 '22 10:10

Billal Begueradj