[英]Clearing a tkinter canvas containing a Matplotlib graph
I am making a User Interface where the user has to put data in a frame, then clicking a button creates a Matplotlib interface. 我正在创建一个用户界面,用户必须将数据放入一个框架中,然后单击一个按钮来创建Matplotlib界面。 I was able to make it work, and my graph shows up along with the toolbar. 我能够使其正常工作,并且图形和工具栏一起显示。
But when I click the button again, I can't get the other graph to go away. 但是,当我再次单击该按钮时,我无法使其他图形消失。 I saw I could use the .delete to clear the canvas. 我看到我可以使用.delete清除画布。 Putting it as the first line of the function will give me the "'canvas' used before assignement". 将其作为函数的第一行将给我“分配前使用的'画布'”。 I tried putting it as global but that didn't seem to work either. 我尝试将其设置为全局,但这似乎也不起作用。
Here is the problematic part of the code: 这是代码中有问题的部分:
def graphiqueMatplot():
#Not included: computation of the coordinates xS, yS,...
f = Figure(figsize=(5,5), dpi=100)
canvas = FigureCanvasTkAgg(f, master=courbe)
a = f.add_subplot(111)
a.plot(xS, yS)
a.plot(xT, yT)
a.plot(xL, yL)
a.axis('tight')
canvas.show()
canvas.get_tk_widget().pack(side='top', fill=Y, expand=1)
toolbar = NavigationToolbar2TkAgg( canvas, courbe )
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
#Main Program
cadreSimulation=Labelframe(root, text='Simulation')
courbe=Canvas(cadreSimulation, height=500, width=500, cursor='trek')
boutonGraphiqueMatplot=Button(cadreSimulation, text='Launch Matplotlib', command=graphiqueMatplot)
boutonGraphiqueMatplot.pack(side='top', fill='x')
courbe.pack()
cadreSimulation.pack(side='left')
Are you able to help ? 你能帮忙吗? Thanks a lot ! 非常感谢 !
I want to add that I am a begginer with programmation, and not a native english speaker, so if you see any mistake concerning those don't hesitate telling me ! 我想补充一点,我是编程的开始者,而不是说英语的母语人士,因此,如果您发现有关这些方面的任何错误,请不要犹豫告诉我!
You have two main options here. 您在这里有两个主要选择。
FigureCanvasTkAgg
object outside the graphiqueMatplot()
function, have that function take that object as a parameter, and then have the function reconfigure the canvas with the new graph. 在graphiqueMatplot()
函数外部创建FigureCanvasTkAgg
对象,让该函数将该对象作为参数,然后让该函数使用新图形重新配置画布。 canvas = FigureCanvasTkAgg(f, master=courbe)
# more canvas creation and packing
...
boutonGraphiqueMatplot=Button(cadreSimulation, text='Launch Matplotlib',
command=lambda: graphiqueMatplot(canvas))
...
def graphiqueMatplot(c):
f = Figure(figsize=(5,5), dpi=100)
c.config(figure=f) # this is how tkinter objects are reconfigured,
# but it may be different for FigureCanvasTkAgg objects!
a = f.add_subplot(111)
# more plotting statements here
delete()
function like you wanted to, catching the exception: 尝试使用delete()
函数,捕获异常: def graphiqueMatplot():
#Not included: computation of the coordinates xS, yS,...
try:
delete canvas/objects # use the delete method here
except:
pass # leave this as pass, so it takes no action if there's an error
f = Figure(figsize=(5,5), dpi=100)
canvas = FigureCanvasTkAgg(f, master=courbe)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.