Using tkinter — How to clear FigureCanvasTkAgg object if exists or similar?

前端 未结 1 1160
天涯浪人
天涯浪人 2021-01-20 17:40

Trying to create a tkinter based window that allows user to create a chart on button click, refreshing the chart -- not adding another, each time. All without creating a new

相关标签:
1条回答
  • 2021-01-20 18:32

    For your current setup, just add a try clause at the start of your plot function.

    def plot(self):   
        try: 
            self.wierdobject.get_tk_widget().pack_forget()
        except AttributeError: 
            pass                
        f=Figure(figsize=(5,1)) 
        aplt=f.add_subplot(111)       
        aplt.plot([1,2,3,4]) 
        self.wierdobject = FigureCanvasTkAgg(f, master=self.frame1) 
        self.wierdobject.get_tk_widget().pack() 
        self.wierdobject.draw()    
    
    0 讨论(0)
提交回复
热议问题