为什么pyplot在osx上给我一个TclError?

时间:2015-12-21 22:03:44

标签: macos matplotlib tkinter pip homebrew

更新2:

我用pip卸载并重新安装,现在发生这种情况。似乎tk没有正确安装。

$ ipython
Python 2.7.11 (default, Dec 21 2015, 14:13:54) 
Type "copyright", "credits" or "license" for more information.

IPython 4.0.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from matplotlib import pyplot as plt
objc[66269]: Class TKApplication is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[66269]: Class TKMenu is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[66269]: Class TKContentView is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[66269]: Class TKWindow is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.

In [2]: plt.plot([1,2,3], [6,5,4])
Out[2]: [<matplotlib.lines.Line2D at 0x110434410>]

In [3]: plt.show()
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 283, in resize
    self.show()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 355, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 30, in blit
    id(data), colormode, id(bbox_array))
TclError

更新:

我使用的是conda python。但是,我仍然在brew python中出现simillar错误

>>> from matplotlib import pyplot as plt
objc[66099]: Class TKApplication is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[66099]: Class TKMenu is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[66099]: Class TKContentView is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[66099]: Class TKWindow is implemented in both /usr/local/opt/tcl-tk/lib/libtk8.6.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
>>> plt.plot([1,2,3],[2,3,4])
[<matplotlib.lines.Line2D object at 0x116d81dd0>]
>>> plt.show()
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 278, in resize
    self.show()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 350, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 24, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError

原始邮寄

当我导入pyplot时,我得到了一个与Tkinter相关的错误。这只是一个渐渐消失的信息。

In [1]: from matplotlib import pyplot as plt
objc[43731]: Class TKApplication is implemented in both /Users/kilojoules/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[43731]: Class TKMenu is implemented in both /Users/kilojoules/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[43731]: Class TKContentView is implemented in both /Users/kilojoules/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.
objc[43731]: Class TKWindow is implemented in both /Users/kilojoules/anaconda/lib/libtk8.5.dylib and /System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the two will be used. Which one is undefined.

当我制作一个情节时,我得到一个奇怪的时间戳和没有情节的白色窗口,以及TclError。为什么会这样?我正在使用brew python(使用--use-brewed-tk标志安装)和pip matplotlib。

In [2]: plt.plot([1,2,3], [2,4,5])
Out[2]: [<matplotlib.lines.Line2D at 0x107f94d50>]

In [3]: 2015-12-21 13:55:02.203 python[43731:912293] setCanCycle: is deprecated.  Please use setCollectionBehavior instead


In [3]: plt.show()
2015-12-21 13:55:10.924 python[43731:912293] setCanCycle: is deprecated.  Please use setCollectionBehavior instead
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/kilojoules/anaconda/lib/python2.7/lib-tk/Tkinter.py", line 1536, in __call__
    return self.func(*args)
  File "/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 278, in resize
    self.show()
  File "/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 350, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 24, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError

这看起来可能是一个conda错误。我永远不应该触及这些东西。

我卸载了anaconda软件包

conda uninstall Tk
conda uninstalal matplotlib

然后我得到了dupes tk并重新安装了python(based on this github issue

brew install homebrew/dupes/tcl-tk
brew uninstall python
brew install python --with-brewed-tk

我现在可以毫无问题地导入pyplot

In [1]: from matplotlib import pyplot as plt

但我得到了同样奇怪的错误。如何正确安装tkinter?

In [2]: plt.plot([1,2,3], [2,4,5])
---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
<ipython-input-2-27b4c4180a02> in <module>()
----> 1 plt.plot([1,2,3], [2,4,5])

/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in plot(*args, **kwargs)
   3090 @_autogen_docstring(Axes.plot)
   3091 def plot(*args, **kwargs):
-> 3092     ax = gca()
   3093     # allow callers to override the hold state by passing hold=True|False
   3094     washold = ax.ishold()

/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gca(**kwargs)
    826     matplotlib.figure.Figure.gca : The figure's gca method.
    827     """
--> 828     ax =  gcf().gca(**kwargs)
    829     return ax
    830 

/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gcf()
    460         return figManager.canvas.figure
    461     else:
--> 462         return figure()
    463 
    464 fignum_exists = _pylab_helpers.Gcf.has_fignum

/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    433                                         frameon=frameon,
    434                                         FigureClass=FigureClass,
--> 435                                         **kwargs)
    436 
    437         if figLabel:

/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     79     FigureClass = kwargs.pop('FigureClass', Figure)
     80     figure = FigureClass(*args, **kwargs)
---> 81     return new_figure_manager_given_figure(num, figure)
     82 
     83 

/Users/kilojoules/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     87     """
     88     _focus = windowing.FocusManager()
---> 89     window = Tk.Tk()
     90     window.withdraw()
     91 

/Users/kilojoules/anaconda/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1812                 baseName = baseName + ext
   1813         interactive = 0
-> 1814         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   1815         if useTk:
   1816             self._loadtk()

TclError: Can't find a usable tk.tcl in the following directories: 
    /System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/Scripts/tk8.5 /System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/Scripts/tk8.5/Resources/Scripts /System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/tk8.5 /System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/tk8.5/Resources/Scripts /Users/kilojoules/anaconda/lib/tk8.5 /Users/kilojoules/anaconda/lib/tk8.5/Resources/Scripts ~/Library/Tcl/tk8.5 ~/Library/Tcl/tk8.5/Resources/Scripts /Library/Tcl/tk8.5 /Library/Tcl/tk8.5/Resources/Scripts /System/Library/Tcl/tk8.5 /System/Library/Tcl/tk8.5/Resources/Scripts /System/Library/Tcl/8.5/tk8.5 /System/Library/Tcl/8.5/tk8.5/Resources/Scripts ~/Library/Frameworks/tk8.5 ~/Library/Frameworks/tk8.5/Resources/Scripts /Library/Frameworks/tk8.5 /Library/Frameworks/tk8.5/Resources/Scripts /System/Library/Frameworks/tk8.5 /System/Library/Frameworks/tk8.5/Resources/Scripts /Users/kilojoules/lib/tk8.5 /Users/kilojoules/anaconda/library



This probably means that tk wasn't installed properly.

2 个答案:

答案 0 :(得分:4)

我在10个小时前面临同样的问题。在我遵循How to import matplotlib in virtual env

中的说明后,问题得以实现

我发现我已经在anacode和system中安装了tk。但我不知道Tk回调的原因。所以我所做的就是改变matplotlib的后端。

cd ~/.matplotlib
vi matplotlibrc

然后改变

backend : TkAgg

backend : Qt4Agg

它对我有用!

答案 1 :(得分:0)

我也必须安装PyQt。 因此,我为解决此问题所做的事情:

  1. 安装PyQt:
    pip install pyqt5
  1. 通过编辑~/.matplotlib/matplotlibrc
  2. 来更改matplotlib的后端
    vi ~/.matplotlib/matplotlibrc

更改

    backend : TkAgg

    backend : Qt5Agg