matplotlib Python3 ImportError:Ubuntu上没有名为'_tkinter'的模块[重复]

92dk7w1h  于 2023-05-01  发布在  Python
关注(0)|答案(1)|浏览(106)

此问题已在此处有答案

"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm(31答案)
Why does tkinter (or turtle) seem to be missing or broken? Shouldn't it be part of the standard library?(3个答案)
昨天关门了。
Python 3的当前版本是3。5.2当我导入matplotlib时,它返回了以下错误

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

然后进口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'
>>>

看来tkinter已经被嵌入了。
我已经通过安装了tk和tcl

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

操作系统是Ubuntu 14。04.
我认为这就是TK没有在Python3上配置的原因,但我不确定。很多人说我应该用tk重建和重新安装Python3,但是我不认为这是一个解决这个问题的优雅方法。
如何解决此问题?

webghufk

webghufk1#

如果你在使用matplotlib后端时遇到问题,请尝试选择一个不同的后端。
Matplotlib迎合了许多不同的场景和用途。
在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

,如果您要创建图像文件而不是显示它

用途:

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

编辑:
根据您的评论,尝试这个,看看您是否得到了一个有效的结果。

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

相关问题