Question

How can I update a matplotlib figure that is a child of a frame in another class in tkinter? Here is where I am stuck.

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
Was it helpful?

Solution

You have to in a way or another retrieve your figure object f from show method.

  • pass f to B constructor
  • class B:
        def __init__(self, frame, figure):
            self.figure = figure
        ...
        def show(self, frame):
            ...
            self.figure.plot( something )
    

  • add f as an attribute of your frame
  • class A:
        def__init__(self, master):
            ...
            sframe.fig = f
            data_obj = B(sframe)
    class B:
        def show(self, frame):
            ...
            frame.fig.plot( something )
    
    Licensed under: CC-BY-SA with attribution
    Not affiliated with StackOverflow
    scroll top