简体   繁体   中英

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.

run from terminal:

sudo apt-get install python3.6-tk

or just reinstall completly:

sudo apt-get install python3.6

Python 版本 3.6.4(Ubuntu 18.04 LTS)

sudo add-apt-repository main

sudo apt-get install python3-tk

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

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM