matplotlib.animation

系统教程 行业动态 更新时间:2024-06-14 17:01:34
matplotlib.animation - AttributeError:'NoneType'对象没有属性'new_timer'(matplotlib.animation - AttributeError: 'NoneType' object has no attribute 'new_timer')

下面的代码是我编写的代码的片段,它基本上在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)

The code below is a snippet of the code i have written, It basically shows a graph in a tkinter window but then struggles when it comes to adding animation(updating) the graph. The following is the error i keep getting, any help would be much appreciated.

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'

The snippet of my code:

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)

最满意答案

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

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

将matplotlib canvas嵌入到tkinter GUI中 - 绘图没有显示,但没有抛出错误 蟒蛇。 使用animation.FuncAnimation时出错

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

The error basically tells you that the figure does not have a canvas.

To prevent that you need to make sure to only start the animation once the figure is added to the FigureCanvasTkAgg. Since we lack the complete code in the question, I can only link you to some other complete examples that are reported to work:

Embedding matplotlib canvas into tkinter GUI - plot is not showing up, but no error is thrown Python. Error using animation.FuncAnimation

In the second case the code from the question produces exactly the same error and a possible solution is to incorporate the animation in the tk class.

更多推荐

本文发布于:2023-04-20 18:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/5b41e26ec6363068734006a6fcf36fdd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:matplotlib   animation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!