我遇到了与你类似的问题,这里我将详细说明问题以及我是如何解决它的。
在Ubuntu 16.04 LTS上,我有Python 3.5.2和Python 2.7.12,但我想尝试Python3.6(出于各种原因,例如像
这个),因此我依赖于这个
post:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
当我尝试运行一个使用tkinter的模块时,出现了以下错误信息:
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
我尝试按照上面的提示安装tkinter:
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.
显然,我仍然不能在Python 3.6中使用tkinter。如何解决这个问题?
我的第一次尝试没有成功:
sudo apt-get install python36-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python36-tk
第二个有效:
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) ...
这解决了我的问题:
~/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
>>>
from tkinter import *
- johnson lai