title: Python: Tkinter and pyplot to replot data
body: <p>I am trying to write a simple program that reads the values of three different widgets and plots a graph of a function depending on what the input values are. I run into the following problems:</p>
  
  <p>1) On the button press I get the error "App_Window instance has no attribute 'refreshFigure'"</p>
  
  <p>2)On exiting ipython (I use ipython-listener in conjunction with a gedit plugin) I get the error "Exception RuntimeError: 'main thread is not in main loop' in del of 
  
  <p>I've based this mostly on this thread - <a href="https://stackoverflow.com/questions/9997869/interactive-plot-based-on-tkinter-and-matplotlib">Interactive plot based on Tkinter and matplotlib</a> and this thread - <a href="https://stackoverflow.com/questions/11270078/how-do-i-refresh-a-matplotlib-plot-in-a-tkinter-window">How do I refresh a matplotlib plot in a Tkinter window?</a></p>
  
  <pre class="lang-py prettyprint-override"><code>#!/usr/bin/python
  from Tkinter import Tk, Frame
  from Tkinter import *
  import Tkinter
  from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  #this bit I added just because the NVC13 module is in another directory
  import sys
  for i in range(len(sys.path)):
      if sys.path[i]!='/home/captain-pants/NV_centri':
          if i==len(sys.path)-1:
              sys.path.append('/home/captain-pants/NV_centri')
          continue        
      else:
          break
  
  import multiprocessing as mp 
  import numpy as np
  import matplotlib.pyplot as plt
  #This module contains all the code necessary to return a list of values that depend on the input parameters
  import NVC13_1lev_advanced_ex_nocoh as C13
  from matplotlib.figure import Figure
  
  #this function takes a list of parameters and returns a list of values dependant on those parameters.
  def plotfunc(c13,Eg,Bz):
      Epam=[]
      nv = C13.NVC13(c13,[2580,1423],[Eg,Eg],0,0,Bz,0)
      evals = nv.intH()
      for i in range(len(evals[0])):
          Epam.append(np.real_if_close(evals[0][i]))
      return Epam
  
  class App_Window:
      def __init__(self, parent):
          frame = Tkinter.Frame(parent)
          self.Egdata=DoubleVar()
          self.c13input=IntVar()
          self.Bzdata=DoubleVar()
          parent.title("Simple")
          Tkinter.Button(text='lulz',command=self.main1).grid(row=3,column=0)
          Tkinter.Label(textvariable=self.Egdata).grid(row=5,column=0)
          self.c13 = Tkinter.Entry(textvariable=self.c13input)
          self.Eg = Tkinter.Scale(orient='horizontal')
          self.Bz = Tkinter.Scale(orient='horizontal')
          self.c13.grid(row=6,column=0)
          self.Bz.grid(row=5,column=0)
          self.Eg.grid(row=4,column=0)
          Fig = Figure()
          FigSubPlot = Fig.add_subplot(111)
          self.line, = FigSubPlot.plot(range(10),'bo')
          x=[]
          y=[]
          self.canvas = FigureCanvasTkAgg(Fig, master=parent)
          self.canvas.show()
          self.canvas.get_tk_widget().grid(row=0,column=0,columnspan=2)
          frame.grid(row=0,column=0)
      def refreshFigure(self,y):
          x=np.arange(len(y))
          self.line.set_xdata(x)
          self.line.set_ydata(y)
          self.canvas.draw()
      def main1(self):
          self.c13input=int(self.c13.get())
          self.Egdata=self.Eg.get()
          self.Bzdata=self.Bz.get()
          values = plotfunc(self.c13input,self.Egdata,self.Bzdata)
              #Just to see whether I actually get the right thing in the input.
          print self.c13input
          self.refreshFigure(values)
  
  root = Tkinter.Tk()
  app = App_Window(root)
  root.mainloop()
  </code></pre>