title: kivy to run python code with threads and matplotlib
body: <p>I have created this python code:</p>
  
  <pre><code>import threading
  from drawnow import drawnow
  import matplotlib.pyplot as plt
  import random
  import time
  
  stop = []
  timer = []
  times = []
  
  plt.ion()
  
  
  class MyThread(threading.Thread):
      def run(self):
          c = 30
          print 'TIMER START'
          while not stop:
              timer.append(c)
              time.sleep(1)
              c -= 1
          print 'TIMER STOP', c, ","
          times.append(c)
          return c
  
  def makefig():
      #plt.ylim(0,5)
      plt.plot(times, 'ro-', label='testgraph')
      plt.grid(True)
  
  def main_loop():
      i = 0
      while True:
          i += 1
          time.sleep(random.randint(0,3))
          if i == 7:
              i = 0
          yield i
  
  if __name__ == '__main__':
      z = main_loop()
      for x in z:
          print x
          print times
          drawnow(makefig)
          if x == 2:
              m = MyThread()
              m.start()
          if x == 5:
              stop.append('a')
          if x == 6:
              stop.pop(0)
              timer = []
  </code></pre>
  
  <p>it let's me to return numbers from 0 to 7 with different time values between generating new number and run countdown timer between values 2 &amp; 5 and append counter value to a list. I am plotting list values in matplotlib.</p>
  
  <p>Question:</p>
  
  <p>how to execute this code with kivy so kivy displays the counter value and not terminal, so kivy plot the value and not matplotlib ?</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>This is my code so far but I can not work out the same threads functionality:</p>
  
  <pre><code>import threading
  from kivy.app import App
  from kivy.uix.boxlayout import BoxLayout
  from kivy.properties import NumericProperty
  
  
  import threading
  from drawnow import drawnow
  import matplotlib.pyplot as plt
  import random
  import time
  
  
  class MyThread(BoxLayout):
      #stop = []
      #timer = []
      #times = []
      i = NumericProperty(0)
  
      def run(self):
          c = 30
          print 'TIMER START'
          while not stop:
              timer.append(c)
              time.sleep(1)
              c -= 1
          print 'TIMER STOP', c, ","
          times.append(c)
          return c
  
      def main_loop(self):
  
          while True:
              self.ids.lbl.text = "{}".format(self.i)
              print self.i
              self.i += 1
              time.sleep(random.randint(0,3))
              if self.i == 7:
                  self.i = 0
  
      def read_it(self):
          threading.Thread(target = self.main_loop).start()
          print 'started'
          if self.i == 2:
              print "Counter start"
          if self.i == 5:
              print "Counter stop"
  
  
  class MyApp(App):
      def build(self):
          self.load_kv('thread.kv')
          return MyThread()
  
  if __name__ == "__main__":
      app = MyApp()
      app.run()
  </code></pre>
  
  <p>EDIT2:</p>
  
  <p>I have forgoten to show thread.kv file:</p>
  
  <pre><code>&lt;MyThread&gt;:
      Button:
          text: "use thread"
          on_release: root.read_it()
      Label:
          id: lbl
          text: "Numbers"
  </code></pre>