如果这个问题很简单,我很抱歉,但我一直在寻找所有我想到的解决这个问题但无济于事的事情。
基本上我有一个微分方程求解器模拟板中的热流,每次点击Tkinter按钮,我都想运行一个新的模拟/动画。问题是,在多次调用新模拟之后程序会变得不稳定。
当我允许用户指定动画是否会连续循环时,我知道出现了问题。如果允许动画循环,后续模拟(循环与否)将减慢并且CPU将保持加载状态,就好像动画在应该被替换为新动画之后仍然在后台某处循环。
以下是我的代码的相关部分,如果我正在做一些愚蠢的事情,请告诉我。
class GUI(object):
def __init__(self,root):
self.print_button = tk.Button(self.root, text='Run Simulation',
command=self.run).grid()
def run(self):
simulation = HeatSim(self)
simulation.compute(self)
simulation.animate(self)
class HeatSim(GUI):
def __init__(self,gui):
# Initialize variables here
def compute(self,gui):
# Compute values of data matrix
def animate(self,gui):
frames = 100
fig = plt.figure()
ax = fig.add_subplot(111)
# Canvas used to display the animation embedded within a tkinter window
canvas = FigureCanvasTkAgg(fig, master=gui.root)
canvas.show()
canvas.get_tk_widget().grid()
images = []
for i in range(frames):
im = ax.imshow(data[:,:,i],cmap='hot',animated=True)
images.append([im])
anim = animation.ArtistAnimation(fig,images,interval=0,blit=True,
repeat=0)
canvas.show()
# Main program loop
root = tk.Tk()
gui = GUI(root)
root.mainloop()