title: Python Tkinter - closing a child window with an exit button
body: <p>I have a Raspberry Pi with the Piface adaptor board. I have made a GUI which controls the LED's on the Piface board.
  One button on the GUI opens a new window which, on the press of a button, starts and stops running a small piece of code to make the LED's run up and down continuously, like Knight Riders car, using a While loop in a thread.
  In this new window I have added a EXIT button.  I want to add a piece of code that will close the new window when I click the EXIT button, and then return to the main window.
  I have looked up many examples but just can't quite see what I should put or where.  I have tried the 'quit' but it closed the whole program.
  Having looked at many examples I maybe creating my new window in not quite the right way so feel free to tell me if there are better ways.</p>
  
  <p>So is there a better way of doing it? Any pointers would be appreciated.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Heres a piece of the code....</p>
  
  <pre><code>   def new_window(self):
          print('New Window')
          self.newWindow = tk.Toplevel(self.master)
          self.app = App2(self.newWindow)
          self.newWindow.grab_set()   # I added this line to stop opening multiple new windows
  
  class App2:
  
  
      def __init__(self, master):
  
              frame = Frame(master)
              frame.pack()
              Label(frame, text='Turn LED ON').grid(row=0, column=0)
              Label(frame, text='Turn LED OFF').grid(row=0, column=1)
  
              self.button0 = Button(frame, text='Knight Rider OFF', command=self.convert0)
              self.button0.grid(row=2, column=0)
              self.LED0 = Label(frame, image=logo2)
              self.LED0.grid(row=2, column=1)
  
              self.button9 = Button(frame, text='Exit', command=self.close_window)
              self.button9.grid(row=3, column=0)
  
  
      def convert0(self, tog=[0]):
  
          tog[0] = not tog[0]
          if tog[0]:
              print('Knight Rider ON')
              self.button0.config(text='Knight Rider ON')
              t=threading.Thread(target=self.LED)
              t.start()
              self.signal = True    #added to stop thread
              self.LED0.config(image = logo)
          else:
              print('Knight Rider OFF')
              self.button0.config(text='Knight Rider OFF')
              self.signal = False   #added to stop thread
              self.LED0.config(image = logo2)
  
      def LED(self):
              while self.signal:   #added to stop thread
  
                  a=0
  
                  while self.signal:   #added to stop thread
                          pfio.digital_write(a,1) #turn on
                          sleep(0.05)
                          pfio.digital_write(a,0) #turn off
                          sleep(0.05)
                          a=a+1
  
                          if a==7:
                                  break
  
                  while self.signal:   #added to stop thread
  
                          pfio.digital_write(a,1) #turn on
                          sleep(0.05)
                          pfio.digital_write(a,0) #turn off
                          sleep(0.05)
                          a=a-1
  
                          if a==0:
                                  break
  
      def close_window(self):
          print('Close Child window')
          #self.newWindow.destroy()   Not sure what to put here?
  </code></pre>