首页 文章

尝试在使用类时在Tkinter中运行实时matplotlib图 . 无法解决TypeError

提问于
浏览
0

我正在尝试使用类结构在Tkinter中使用实时matplotlib图 . 我正在使用此stackoverflow question中的代码,该代码在Tkinter中成功运行matplotlib图形(不使用类结构) . 每当我尝试运行我改变的代码时,我都会得到一个

TypeError: __init__ takes exactly 2 arguments (1 given)

我知道之前已经问过这个TypeError类型的问题 . 我尝试使用this但提供的答案并没有解决我的问题 .

似乎是问题的代码行是:

ani = animation.FuncAnimation(Window().fig, Window().animate(), interval=1000, blit=Fals

我试图改变我如何称为无花果和动画但似乎没有任何效果 .


import Tkinter as Tk
from matplotlib import pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
xar = []
yar = []

class Window:
    def __init__(self,master):
        frame = Tk.Frame(master)
        fig = plt.figure(figsize=(14, 4.5), dpi=100)

        self.ax = fig.add_subplot(1,1,1)
        self.ax.set_ylim(0, 100)
        self.line, = self.ax.plot(xar, yar)

        self.canvas = FigureCanvasTkAgg(fig,master=master)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        frame.pack()
    def animate(self,i):
        yar.append(99-i)
        xar.append(i)
        self.line.set_data(xar, yar)
        self.ax.set_xlim(0, i+1)

root = Tk.Tk()
ani = animation.FuncAnimation(Window().fig, Window().animate(),interval=1000, blit=False)
app = Window(root)
root.mainloop()

2 回答

  • 1

    你有很多错误:

    正如Paul Comelius所说,你必须创建实例

    app = Window(root)
    ani = animation.FuncAnimation(app.fig, app.animate, interval=1000, blit=False)
    

    你必须在类中使用 self.fig 以便稍后将其作为 app.fig

    animation.FuncAnimation 期望回调 - 它表示没有 () 的函数名称 .
    在你的代码中它将是 app.animate .

    ani = animation.FuncAnimation(app.fig, app.animate, interval=1000, blit=False)
    

    完整的工作代码

    import Tkinter as Tk
    from matplotlib import pyplot as plt
    import matplotlib.animation as animation
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    
    class Window:
    
        def __init__(self,master):
            frame = Tk.Frame(master)
    
            self.fig = plt.figure(figsize=(14, 4.5), dpi=100)
    
            self.ax = self.fig.add_subplot(1,1,1)
            self.ax.set_ylim(0, 100)
            self.line, = self.ax.plot(xar, yar)
    
            self.canvas = FigureCanvasTkAgg(self.fig, master=master)
            self.canvas.show()
            self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
            frame.pack()
    
        def animate(self,i):
            yar.append(99-i)
            xar.append(i)
            self.line.set_data(xar, yar)
            self.ax.set_xlim(0, i+1)
    
    xar = []
    yar = []
    
    root = Tk.Tk()
    app = Window(root)
    ani = animation.FuncAnimation(app.fig, app.animate, interval=1000, blit=False)
    root.mainloop()
    
  • 1

    我不确定你要做什么,但你可能想要这样的东西:

    root = Tk.Tk()
    app = Window(root)
    ani = animation.FuncAnimation(app.fig, app.animate(),interval=1000, blit=False)
    root.mainloop()
    

    这不会完全解决问题,因为 animate 函数也没有参数 .

相关问题