如何清除或覆盖Tkinter画布?

时间:2018-08-15 09:28:08

标签: python matplotlib tkinter

下面的代码显示了我当前正在使用的tkinter GUI的一页。

我想要“清除绘图字段”按钮的含义是:清除画布,因为如果再次绘图,新绘图将打包在下面。

或者:我该如何覆盖现有图,以便摆脱按钮本身?

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")

3 个答案:

答案 0 :(得分:1)

仅使用Google搜索“ tkinter透明画布”就可以thisthisthisthis

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

答案 1 :(得分:0)

我会destroy()画布,然后重新运行plot()。为此,您需要使canvas这样的类属性self.canvas。现在我们有了class属性,您的任何方法都可以使用self.canvas而不会出现问题。

看看我从您的问题中修改的这段代码,如果您有任何问题,请告诉我。

class PlotPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.canvas = None

        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):
        if self.canvas == None:
            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,)

            self.canvas = FigureCanvasTkAgg(f, self)
            self.canvas.show()
            self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

            toolbar = NavigationToolbar2TkAgg(self.canvas, self)
            toolbar.update()
            self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

            print("Stuff has been plotted")
        else:
            print("Plot already plotted please clear first")

    def clearPlotPage(self):
        self.canvas.destroy()
        self.canvas = None
        self.plot()
        print("Plot Page has been cleared")

答案 2 :(得分:0)

对于仍在处理AttributeError的任何人:'FigureCanvasTkAgg'对象没有属性'destroy'。

我通过创建一个“虚拟”容器框架来包装FigureCanvas来解决此问题,该框架可以销毁,从而销毁FigureCanvas。

相关问题