matplotlib.animation - AttributeError:' NoneType'对象没有属性' new_timer'

时间:2017-03-22 23:29:04

标签: python matplotlib tkinter

下面的代码是我编写的代码的片段,它基本上在tkinter窗口中显示了一个图形,但在添加动画(更新)图形时却很困难。以下是我不断得到的错误,非常感谢任何帮助。

Traceback (most recent call last):
  File "C:\Users\nasto\Documents\Company\CMSFRAME.py", line 163, in <module>
    ani = animation.FuncAnimation (f, animate, interval=1000)
  File "C:\Users\nasto\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1462, in __init__
    TimedAnimation.__init__(self, fig, **kwargs)
  File "C:\Users\nasto\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1225, in __init__
    event_source = fig.canvas.new_timer()
AttributeError: 'NoneType' object has no attribute 'new_timer'

我的代码片段:

      canvas =  FigureCanvasTkAgg(f, self)
      canvas.show()
      canvas.get_tk_widget().pack(side=tk.LEFT,  fill=tk.BOTH, expand = True)
      toolbar = NavigationToolbar2TkAgg(canvas, self)
      toolbar.update()
      canvas._tkcanvas.pack(side=tk.LEFT,  fill=tk.BOTH, expand = True)

f = Figure(figsize = (5,5), dpi=100)
a = f.add_subplot(111)

def  animate(interval):
    pullData = open('financeData.txt','r').read()
    dataList = pullData.split('\n')
    xList = []
    yList=[]
    for eachLine in dataList:
        if len(eachline) >1:
            x,  y = eachLine.split(',')
            xList.append(int(x))
            yList.append(int(x))
    a.clear()
    a.plot(xList, yList)

app = start()
ani = animation.FuncAnimation (f, animate, interval=1000)

1 个答案:

答案 0 :(得分:3)

错误基本上告诉你图中没有画布。

为防止您需要确保仅在将图形添加到FigureCanvasTkAgg后才启动动画。由于我们缺少问题中的完整代码,因此我只能将您链接到其他报告可用的完整示例:

  1. Embedding matplotlib canvas into tkinter GUI - plot is not showing up, but no error is thrown
  2. Python. Error using animation.FuncAnimation
  3. 在第二种情况下,问题中的代码产生完全相同的错误,可能的解决方案是将动画合并到tk类中。