从另一个类更新matplotlib地物

从另一个类更新matplotlib地物,matplotlib,tkinter,figure,Matplotlib,Tkinter,Figure,如何更新作为tkinter中另一类中帧的子对象的matplotlib图形?这就是我被困的地方 class A(): def__init__(self, master): sframe = Frame(master) sframe.pack(side=RIGHT) f = Figure(figsize=(3,2), dpi=100) a = f.add_subplot(122); # initially pl

如何更新作为tkinter中另一类中帧的子对象的matplotlib图形?这就是我被困的地方

class A():
    def__init__(self, master):
        sframe = Frame(master)
        sframe.pack(side=RIGHT)
        f = Figure(figsize=(3,2), dpi=100)

        a = f.add_subplot(122);
        # initially plots a sine wave 
        t = arange(0.0, 1, 0.01);
        s = sin(2*pi*t);
        a.plot(t,s);

        canvas = FigureCanvasTkAgg(f, master=sframe)
        canvas.show()
        canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
        # now i create the other object that will call show()
        data_obj = B(sframe)


class B():
    ...
    show(self, frame):
       _wlist = frame.winfo_children()
       for item in _wlist:
           item.clear() # or something like this
       # and then re-plot something or update the current plot

您必须以某种方式从show方法中检索地物对象
f

  • 将f传递给
    B
    构造函数
  • class B:
        def __init__(self, frame, figure):
            self.figure = figure
        ...
        def show(self, frame):
            ...
            self.figure.plot( something )
    
  • 添加f作为帧的属性
  • class A:
        def__init__(self, master):
            ...
            sframe.fig = f
            data_obj = B(sframe)
    class B:
        def show(self, frame):
            ...
            frame.fig.plot( something )
    

    您只需要获取对地物或轴对象的引用。当心从多个线程与对象对话,这可能不起作用(但我认为这与gui框架中的限制有关)。感谢tcaswell,我需要一个框架参考,因为我需要修改框架中的多个图形…感谢@FabianAndre的建议。问题是我的框架中有多个图形,所以我需要访问特定的图形。这就是我尝试winfo_children的原因。但是,我无法处理在调用winfo_class()时返回“Canvas”的子级。我想要一种访问与此画布关联的图f的方法,以便能够更新我之前绘制的内容(在本例中为正弦波)。我如何访问该图以清除/更新它?我以类似的方式解决了它。我没有将图形添加到框架中,而是添加了轴以及与图形相关联的画布。因此,现在要更新它,我执行以下操作:sframe.ax=a,并在show()中执行一次:sframe.ax.clear()sframe.ax.plot(newline)sframe.canvas.show()是否有更优雅的方法?