The code below shows one Page of a tkinter GUI I'm currently working on.
What I want the 'Clear Plot Field'-Button to do is what it says: Clear the canvas, because if I plot again the new plot is packed below.
OR: How can I overwrite an existing plot, so I can get rid of the button itself?
class PlotPage(tk.Frame):
"""Page containing the matplotlib graphs"""
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="PlotPage", font=LARGE_FONT)
label.pack(pady=5)
button1 = ttk.Button(self, text="Back to Start Page", command=lambda: controller.show_frame(StartPage))
button1.pack(pady=5)
buttonPlot = ttk.Button(self, text="Show Plot", command=self.plot)
buttonPlot.pack(pady=5)
buttonClear = ttk.Button(self, text="Clear Plot Field", command=lambda: controller.show_frame(PlotPage))
buttonClear.pack(pady=5)
def plot(self):
"""generate a simple matplotlib graph for multiple profiles"""
f = Figure(figsize=(8,4), dpi=100)
a = f.add_subplot(111)
for profile in listofProfiles:
X_list=profile.indexList
Y_list=profile.VL_List
[...] Some lines related to plotting [...]
a.plot(X_list, Y_list, lw=0.3,)
canvas = FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2TkAgg(canvas, self)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
print("Stuff has been plotted")
def clearPlotPage(self):
"""cleares the whole plot field"""
# ???
print("Plot Page has been cleared")