'Is there an easy way to display Matplotlib subplot animation within Tkinter GUI?

Currently I am only able to display one window or the other, because of the fact I am calling the tkinter mainloop function twice. Once in the application, once to render my plot. This is creating a problem where my main application will not run until the instance of my plot (newWindow) is destroyed.

This class creates the animated plot and renders it.

class newWindow(tkinter.Toplevel):
    def __init__(self):
        w = tk.Toplevel()
        canvas = FigureCanvasTkAgg(fig, master=w)
        canvas.draw()
        w.title("Temperatures")
        w.resizable(0, 0)
        canvas = FigureCanvasTkAgg(fig, master=w)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        toolbar = NavigationToolbar2Tk(canvas, w, pack_toolbar=False)
        toolbar.update()
        anim = animation.FuncAnimation(fig, animate,
                                       fargs=(xs, y1s, y2s, y3s, y4s, y5s, y6s, y7s, y8s, y9s, 
                                       y10s, y11s, y12s),
                                       interval=1000)
        width = w.winfo_screenwidth()
        height = w.winfo_screenheight()
        w.geometry("%dx%d" % (width, height))
        w.mainloop()

I'm trying to pack the graph above into my tkinter mainloop(). Everytime I try to do this using the command below in my main application I can not render the image. My main app pops up in one mainloop and the live plotting appears in a totally separate window. I know this is due to the fact that I'm calling mainloop twice. (which prevents the main window from actually populating fully.) My question is what do I have to do differently in order to place the animated graph inside my mainloop application at runtime and still be able to bring up the other mainloop window.

Below is the call I am making to the newWindow() class within my main application (trying to add this class into a button press)

ttk.Button(root, text='Show Temperatures', command=newWindow()).grid(column=1, row=2)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source