title: Break a while loop with a stop button in tkinter
body: <p>I wrote a <strong>python GUI</strong> which should create and update a <strong>.csv</strong> file by clicking a button called <strong>"Start"</strong> and should stop the while loop which updates .csv by clicking another button called <strong>"Stop"</strong>. But whenever I run the GUI and click on start it freezes. Although, I see that the <strong>.csv</strong> file is continuously updating but I can't stop the .csv from updating new rows. I am just simply running the code using python 2.7 &amp; ubuntu terminal writing <strong>python filename.py</strong>. 
  Can anyone please check what's wrong in my code? </p>
  
  <pre><code>from Tkinter import *
  import datetime
  import sys
  import time
  import csv
  import math
  
  A1 = 0
  
  def csv_write(label):
      global A1
      A1 = 0
      A = str(datetime.datetime.now()) + ".csv"
      start = time.time()
      elapsed = 0
      with open(A, 'wt') as filename:
           csv_writer = csv.writer(filename, delimiter=',')
           csv_writer.writerow(('IMU', 'Time', 'A.Sensor.X', 'A.Sensor.Y',   'A.Sensor.Z', 'G.Sensor.X', 'G.Sensor.Y',
                           'G.Sensor.Z', 'M.Sensor.X', 'M.Sensor.Y', 'M.Sensor.Z',
                           'IMU', 'Time', 'A.Sensor.X', 'A.Sensor.Y', 'A.Sensor.Z', 'G.Sensor.X', 'G.Sensor.Y',
                           'G.Sensor.Z', 'M.Sensor.X', 'M.Sensor.Y', 'M.Sensor.Z',
                           'IMU', 'Time', 'A.Sensor.X', 'A.Sensor.Y', 'A.Sensor.Z', 'G.Sensor.X', 'G.Sensor.Y',
                           'G.Sensor.Z', 'M.Sensor.X', 'M.Sensor.Y', 'M.Sensor.Z',
                           'IMU', 'Time', 'A.Sensor.X', 'A.Sensor.Y', 'A.Sensor.Z', 'G.Sensor.X', 'G.Sensor.Y',
                           'G.Sensor.Z', 'M.Sensor.X', 'M.Sensor.Y', 'M.Sensor.Z',
                           'IMU', 'Time', 'A.Sensor.X', 'A.Sensor.Y', 'A.Sensor.Z', 'G.Sensor.X', 'G.Sensor.Y',
                           'G.Sensor.Z', 'M.Sensor.X', 'M.Sensor.Y', 'M.Sensor.Z',
                           'IMU', 'Time', 'A.Sensor.X', 'A.Sensor.Y', 'A.Sensor.Z', 'G.Sensor.X', 'G.Sensor.Y',
                           'G.Sensor.Z', 'M.Sensor.X', 'M.Sensor.Y', 'M.Sensor.Z'))
           while (A1==0):
  
               elapsed = str(time.time() - start)
               label['text']=elapsed 
               csv_writer.writerow((1, 1, 2, 3,
                               4, 5, 6,
                               7,8, 9,
                               1, 2, 3,
                               4, 5, 6,
                               7, 8,
                               9, 1, 2,
                               3, 4, 5,
                               6, 7, 8,
                               9, 0, 1,
                               2, 3, 4,
                               5, 6, 7,
                               8, 9, 0,
                               1, 2,
                               3, 4, 5,
                               6, 7, 8,
                               9, 0, 1,
                               2, 3,
                               4, 5, 6,
                               7, 8, 9,
                               0, 1, 2,
                               3, 4,
                               5, 6, 7))
  
  
  def stop():  
      global A1
      A1 = 1
  
  root = Tk()
  frame = Frame(root)
  frame.pack()
  root.title("connect and get sensor data")
  root.geometry("500x500")  # You want the size of the app to be 500x500
  root.resizable(0, 0)  # Don't allow resizing in the x or y direction
  label = Label(root, text="Welcome!", fg="black", font="Verdana 15 bold")
  label.pack(side=TOP, padx=5 )
  button = Button(root, text='Start', width=25, command=lambda: csv_write(label))
  button1 = Button(root, text='Stop', width=25, command=lambda: stop())
  button1.pack(side=BOTTOM, pady=10)
  button.pack(side=BOTTOM, pady=10)
  root.mainloop()
  </code></pre>