使用 tkinter -- 如果存在或类似的对象,如何清除 FigureCanvasTkAgg 对象? [英] Using tkinter -- How to clear FigureCanvasTkAgg object if exists or similar?

查看:84
本文介绍了使用 tkinter -- 如果存在或类似的对象,如何清除 FigureCanvasTkAgg 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个基于 tkinter 的窗口,允许用户在单击按钮时创建图表,刷新图表——而不是每次添加另一个.所有这些都无需创建新窗口.想法是单击 -> 创建图表,再次单击 -> 在同一位置用新图表替换图表.没有额外的点击,没有额外的按钮关闭.使用 matplotlib.backends.backend_tkagg 和 FigureCanvasTkAgg.这方面的文档似乎几乎不存在.尝试了 .get_tk_widget() 中的各种属性,看看我是否可以测试它是否已经存在,获取列表等.还尝试清除画布.

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 window. The idea is click -> create chart, click again -> replace the chart with new chart in same spot. No extra clicks, no extra button to close. Using matplotlib.backends.backend_tkagg and FigureCanvasTkAgg. Documentation appears to be virtually non-existent on this. Tried various attributes in .get_tk_widget() to see if I could test if it exists already, get a list, etc. Also tried clearing canvas.

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

class testme:
    def __init__(self,frame1):
        self.frame1=frame1     
        self.button=Button(self.frame1,text="DRAWME",command=self.plot) 
        self.button1=Button(self.frame1,text="CLEARME",command=self.clearme)
        self.button.pack()       
        self.button1.pack()      

    def plot(self):                   
        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()                

    def clearme(self):       
       self.wierdobject.get_tk_widget().pack_forget()     

root=Tk()
aframe=Frame(root)
testme(aframe)
aframe.pack()  #packs a frame which given testme packs frame 1 in testme
root.mainloop()

附加的示例代码几乎接近我的目标,但它需要一个CLEARME"按钮(仅当DRAWME"只单击一次时才有效.我只想要某种 if 语句来检查是否有一个 FigureCanvasTkAgg 对象在框架已经,如果是这样,请删除它而不是单击按钮.

Attached example code almost approximates my goal but it requires a "CLEARME" button (which only works right if "DRAWME" was only clicked once. I just want some kind of if statement that checks if there is a FigureCanvasTkAgg object in the frame already and if so remove it instead of a button click.

经过多次尝试后,我得出结论,我对这里发生的不止一件事情存在根本性的误解.

After a number of attempts I concluded I have a fundamental misunderstanding of more than one thing that's going on here.

推荐答案

对于您当前的设置,只需在 plot 函数的开头添加一个 try 子句即可.

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

这篇关于使用 tkinter -- 如果存在或类似的对象,如何清除 FigureCanvasTkAgg 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆