title: Clearing a tkinter canvas containing a Matplotlib graph
body: <p>I am making a User Interface where the user has to put data in a frame, then clicking a button creates a Matplotlib interface. I was able to make it work, and my graph shows up along with the toolbar.</p>
  
  <p>But when I click the button again, I can't get the other graph to go away. I saw I could use the <em>.delete</em> to clear the canvas. Putting it as the first line of the function will give me the "'canvas' used before assignement". I tried putting it as <em>global</em> but that didn't seem to work either.</p>
  
  <p>Here is the problematic part of the code:</p>
  
  <pre><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')
  </code></pre>
  
  <p>Are you able to help ? Thanks a lot !</p>
  
  <p><em>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 !</em></p>