Install tkinter in python3.6 on Ubuntu [duplicate]
Asked Answered
T

6

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

Trover answered 6/3, 2017 at 10:22 Comment(5)
try from tkinter import *Crew
Just did, gives the same error. @johnsonlai Also, I'm not going to be working with tkinter, matplotlib is.Trover
I know I should, @Marvo, but I need to work on this right away for some short time.Trover
apt get install python3.6-tkMuimuir
@Boki, It doesn't work that way. 12.04's ppa's latest python is 3.2, deadsnakes' ppa's latest is 3.5 and that only has the core python. To be able to apt-get, the package must exist in the ppa.Trover
G
13

run from terminal:

sudo apt-get install python3.6-tk

or just reinstall completly:

sudo apt-get install python3.6
Granular answered 1/8, 2017 at 16:11 Comment(0)
E
11

Python version 3.6.4 (Ubuntu 18.04 LTS)

sudo add-apt-repository main

sudo apt-get install python3-tk
Everything answered 10/11, 2018 at 23:44 Comment(1)
After searching for almost 2 hours, this was the solution that actually worked for me. ThanksBughouse
E
5

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
>>> 
Eu answered 3/5, 2018 at 11:40 Comment(1)
mysteriously this does not work for me. apt install python3.6-tk says "Note, selecting 'python3-tk' instead of 'python3.6-tk'" and then goes ahead and installs python3-tk instead.Dagon
S
1

Try these lines, It might help

os.environ['TCL_LIBRARY'] = r'C:\Users\asus\AppData\Local\Programs\Python\Python36-32\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\asus\AppData\Local\Programs\Python\Python36-32\tcl\tk8.6'

Recheck path before executing.

Sanctity answered 17/8, 2017 at 6:14 Comment(0)
W
1

Python version 3.6.4 (Ubuntu 18.04 LTS)

I was having the same error: tkinter module not found. Even after trying to install via pip $ pip install tkinter I got this error, below

Collecting tkinter
  Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter

I did this tried to install tkinter for Python3.6 by running the command. It worked for me. $ sudo apt-get install python3.6-tk

Whish answered 1/7, 2018 at 4:17 Comment(0)
H
1

Installed python3.8.2 on ubuntu 20.04, tried importing tkinter but failed. Got this error: da0xxx:~/python_trn/pygui$ python3 -m tkinter /usr/bin/python3: No module named tkinter

but ran:

da0xxx:~/python_trn/pygui$ sudo apt-get install python3-tk

now able to import tkinter!

da0xxx:~/python_trn/pygui$ python3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

import tkinter as tk

Thanks above!

Halm answered 20/5, 2020 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.