下面的代码显示了我目前正在处理的 tkinter GUI 的一页。
我希望“清除绘图字段”-按钮执行的操作是它所说的:清除 Canvas ,因为如果我再次绘图,新绘图会打包在下面。
或者:我怎样才能覆盖现有的绘图,这样我就可以摆脱按钮本身?
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")
最佳答案
简单地谷歌搜索“清晰的 tkinter Canvas ”得到了我 this , this , this和 this .
简短回答:调用 canvas.delete('all')
将清除整个 Canvas 。
关于python - 如何清除或覆盖 tkinter Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856163/