code: def runBrain(self, f):
          
          if self.error:
              self.error.value = ""
          def wrapper():
              self.brain_running.set()
              try:
                  f()
              except KeyboardInterrupt:
                  # Just stop
                  pass
              except Exception as e:
                  if self.error:
                      self.error.value = "<pre style='background: #fdd'>" + traceback.format_exc() + "</pre>"
                  else:
                      raise
              finally:
                  self.brain_running.clear()
              # Otherwise, will show error
          threading.Thread(target=wrapper).start()
docs: Run a brain program in the background.