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

Nas*_*son 5 python tkinter matplotlib

下面的代码是我编写的代码片段,它基本上在 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'
Run Code Online (Sandbox Code Playgroud)

我的代码片段:

      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)
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 4

该错误基本上告诉您该图形没有画布。

为了防止这种情况,您需要确保仅在将图形添加到FigureCanvasTkAgg. 由于我们缺乏问题中的完整代码,我只能将您链接到一些据报告有效的其他完整示例:

  1. 将 matplotlib canvas 嵌入 tkinter GUI - 绘图没有显示,但没有抛出错误
  2. Python。使用animation.FuncAnimation时出错

在第二种情况下,问题中的代码会产生完全相同的错误,可能的解决方案是将动画合并到 tk 类中。