我下面的Python代码基于解决方案here。
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.animation as animation
import tkinter as tk
import math
class Application(tk.Frame):
def __init__(self, master):
self.label_0 = tk.Label(root, text="")
tk.Frame.__init__(self,master)
self.direction = 0
self.length = 5.5
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(1,1,1)
ax.set_aspect('equal')
ax.set_ylim(-12,12)
ax.set_xlim(-12,12)
ax.axis('on')
self.vector = plt.arrow(0,0, self.length*math.sin(self.direction), self.length*math.cos(self.direction), width=0.3, head_width=2)
ax.add_patch(self.vector)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0,column=1)
animation.FuncAnimation(fig, self.move_arrow, interval=200, blit=False)
def move_arrow(self):
self.direction += 0.0349
self.length = max(1, self.length-0.05)
self.vector.set_data(self.direction, self.length)
return None
root=tk.Tk()
app=Application(master = root)
app.mainloop()
我想获得一个螺旋旋转的箭头的动画。相反,我得到了下面的固定箭头。
如何为上面的箭头设置动画?
我删除了 Class 的使用,以使您的代码更接近工作解决方案。
代码:
import math
import tkinter as tk
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def move_arrow(i):
root.direction += 0.1349
root.length = max(1, root.length - 0.05)
root.vector.set_data(dx=root.direction, dy=root.length)
root = tk.Tk()
root.label_0 = tk.Label(root, text="")
root.direction = 0
root.length = 5.5
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1, 1, 1)
ax.set_aspect('equal')
ax.set_ylim(-12, 12)
ax.set_xlim(-12, 12)
ax.axis('on')
root.vector = plt.arrow(0, 0, root.length * math.sin(root.direction), root.length * math.cos(root.direction), width=0.3, head_width=2)
ax.add_patch(root.vector)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0, column=1)
ani = animation.FuncAnimation(fig, move_arrow, interval=200, blit=False)
root.mainloop()