【发布时间】:2020-08-13 19:11:10
【问题描述】:
我找到了this thread,但我的情况有点不同,因为我希望我的情节存在于单独的窗口中,因此创建了以下类:
import tkinter as tk
import random
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import pyplot, animation
class Graphs(tk.Toplevel):
sensor_data = []
steps = []
def __init__(self, master): # master here is my root tk window
super().__init__(master)
self.title("Plot me some data")
self.figure = pyplot.figure(dpi=150)
self.sensor = figure.subplots(1, 1, 1)
self.sensor.grid(True)
self.line, = self.sensor.plot(self.steps, self.sensor_data)
self.canvas = FigureCanvasTkAgg(figure, self)
self.canvas.get_tk_widget().grid(column=0, row=0)
def animate(self):
ani = animation.FuncAnimation(self.figure, self.animation, fargs=(self,), blit=False, interval=1000)
"""
This is a dumb logic to visualise if animation FuncAnimation is working
"""
@staticmethod
def animation(i, self):
self.steps.append(i)
self.sensor_data.append(self.steps[-1] * random.randint(-5, 5))
self.line.set_data(self.steps, self.sensor_data[-1])
从主控制器我称之为:
self.graphs = Graphs(self)
self.graphs.animate()
所以,由于动画不工作,我迷路了。尝试在动画函数逻辑中调试代码设置断点,它永远不会停止让我认为动画从未开始的原因。任何提示我在这里错过或做错了什么?
【问题讨论】:
标签: python matplotlib canvas tkinter