I'm practicing some tkinter and matplotlib and I came across with something that I'd like to learn how to do. The idea is simple: for each plot, a new page (a new frame).
It's a simulation of a true scenario that I'm currently dealing with. In this example you should enter the number of plots that are supposed to happen then you click on 'File' > 'Open plot' and then the plotting begins. But the thing is... I know how to embed one graph into one frame, but I'd like to learn how to embed multiple graphs in different frames, so later on I can create a 'next' button which goes to the next frame that contains the next plot.
The following code is not too developed and probably contains mistakes, but I hope it informs the idea:
from tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
x = [1,2,3]
y = [1,2,3]
numberofplots = None
numberofplots = eval(input('Number of plots: '))
class app():
global numberofplots
def __init__(self):
root = Tk()
root.geometry('640x400')
menu_bar = Menu(root)
root.configure(menu = menu_bar)
menu_file = Menu(menu_bar)
menu_bar.add_cascade(label='File',menu = menu_file)
menu_file.add_command(label='Open plots',command = openPlots)
root.mainloop()
def openPlots():
global numberofplots
fig = plt.figure()
for i in range(numberofplots):
plt.plot(x,y)
# numberofplots = number of frames to be created
frame = Frame(root)
frame.pack()
canvas = FigureCanvasTkAgg(fig, frame)
canvas.show()
canvas.get_tk_widget().pack(fill='both', expand=True)
toolbar = NavigationToolbar2TkAgg(canvas, frame)
toolbar.update()
canvas._tkcanvas.pack(fill='both', expand=True)
run = app()