繁体   English   中英

如何清除或覆盖 tkinter 画布?

[英]How can I clear or overwrite a tkinter canvas?

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

简单地谷歌搜索“透明的tkinter画布”就给我这个这个这个这个

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

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

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

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

请分享您创建的用于包装图形画布的容器框架的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM