python - 如何清除或覆盖 tkinter Canvas ?

标签 python matplotlib tkinter

下面的代码显示了我目前正在处理的 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 , thisthis .

简短回答:调用 canvas.delete('all') 将清除整个 Canvas 。

关于python - 如何清除或覆盖 tkinter Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51856163/

相关文章:

python - 在 Python 中检查空文件或丢失文件的正确方法

python - 如何在Python中将日期时间转换为字符串?

Python - 使用 matplotlib.pyplot 制作动画

python - Pandas 箱线图中共享轴的不同 ylim

python - Tkinter:如何在 Canvas 窗口中获取框架以扩展到 Canvas 的大小?

python - 为什么使用 `frame.quit` 关闭 tkinter 子窗口会退出我的应用程序?

python - 在所有其他正在运行的应用程序之上显示 tkMessageBox

python - 将数据表示为傅里叶变换或高次多项式?

python - 如何修复 pandas FutureWarning : inferring datetime64[ns] from data containing strings is deprecated

python - Numpy 索引超出范围错误