简体   繁体   English

Python3 ImportError:在Ubuntu上没有名为'_tkinter'的模块

[英]Python3 ImportError: No module named '_tkinter' on Ubuntu

The current version of Python3 is 3.5.2 when I import matplotlib it retuned the following error 当我导入matplotlib时,当前版本的Python3是3.5.2,它重新调整了以下错误

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python3.5/site-packages/six.py", line 92, in __get__
    result = self._resolve()
  File "/usr/local/lib/python3.5/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python3.5/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'

And import tkinter 并导入tkinter

Python 3.5.2 (default, Jan 19 2017, 11:29:22)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
>>>

It seems that tkinter has already been intalled. 似乎tkinter已被安装。

I have installed tk and tcl by 我已经安装了tk和tcl

sudo apt-get install tk-dev
sudo apt-get install tk8.6-dev

And OS is Ubuntu 14.04. OS是Ubuntu 14.04。

I think it is the reason that TK was not configured on Python3, but I'm not sure. 我认为这是在Python3上没有配置TK的原因,但我不确定。 Many people said I should rebuild and reinstall Python3 with tk, however I don't think it is an elegant way to solve this problem. 很多人说我应该用tk重建并重新安装Python3,但我不认为这是解决这个问题的优雅方法。

How can I fix this problem? 我该如何解决这个问题?

If you are having trouble with a matplotlib backend try selecting a different one. 如果您在使用matplotlib后端时遇到问题,请尝试选择其他后端。
Matplotlib caters for many different scenarios and uses. Matplotlib适用于许多不同的场景和用途。
On Linux, I use the following code to select whichever backend is available and works first. 在Linux上,我使用以下代码来选择可用的后端并且首先工作。

import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        break
    except:
        continue

or if you are going to be creating an image file rather than displaying it 或者如果您要创建图像文件而不是显示它

Use: 采用:

matplotlib.use('agg')
from matplotlib import pyplot as plt

Edit: 编辑:
Based on your comments try this and see if you get a result that works. 根据您的评论尝试这一点,看看你是否得到了一个有效的结果。

import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
print ("I will test for", gui_env)
for gui in gui_env:
    print ("testing", gui)
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        print ("    ",gui, "Is Available")
        plt.plot([1.5,2.0,2.5])
        fig = plt.gcf()
        fig.suptitle(gui)
        plt.show()
        print ("Using ..... ",matplotlib.get_backend())
    except:
        print ("    ",gui, "Not found")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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