AttributeError: 'MechanismAnimation' object has no attribute 'update'
时间: 2023-08-01 18:11:42 浏览: 145
抱歉,我之前给出的代码有误。问题出在调用`super().__init__(self.fig, self.update, frames=frames, interval=interval)`时,它会尝试调用父类`FuncAnimation`的`update`方法,但是我们没有重写该方法。
为了解决这个问题,我们需要将`MechanismAnimation`类的`update`方法重命名为另一个名称,例如`animate`。同时,我们还需要在`super().__init__`中传递该新名称的引用。
请使用以下修改后的代码:
```python
class MechanismAnimation(animation.FuncAnimation):
def __init__(self, mechanism, frames=100, interval=50):
self.mechanism = mechanism
self.fig, self.ax = plt.subplots()
self.lines = []
for link in self.mechanism.links:
line, = self.ax.plot([], [], lw=2)
self.lines.append(line)
super().__init__(self.fig, self.animate, frames=frames, interval=interval)
def animate(self, frame):
for i, link in enumerate(self.mechanism.links):
x = [link.start.x, link.end.x]
y = [link.start.y, link.end.y]
self.lines[i].set_data(x, y)
```
请确保在调用`super().__init__`时传递的是修正后的方法名`animate`。
这样修改后,应该能够成功创建机械机构动画并显示出来。如果还有其他问题,请随时提问。感谢你的理解!
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)