title: Python script keeps running in the background
body: <p>This is a simple script that is used to calculate Z scores of some neuropsychological tests. </p>
  
  <p>But recently the code seems to be running on the background even after exiting the program. This problem didn't exist before, I use the following startup piece to ensure program is running with elevation, and is correct size for the display.</p>
  
  <p>Program can be shut down using a "exit" command that raises SystemExit or just by pressing the X button on the top bar. Program keep running on the background regardless.</p>
  
  <p>Where did I go wrong?</p>
  
  <pre><code>def progStructure():
      first_run = True
      if first_run:
          import os
          os.system("mode con: cols=160 lines=50")
          print("""
      ================================================
      PROGRAM GREETING
      ================================================
      """)
  
          mainStartup()
          first_run = False
  
      while settings("auto_run"):
          mainStartup()
          #this function contains the main program, but irrelevant to the question
  
      print("Auto shutdown enabled, program is shutting down.")
  
      wait(2)
      exit()
          #informs the user data has been saved then restarts
  
  
  import ctypes, sys
  
  def is_admin():
      try:
          return ctypes.windll.shell32.IsUserAnAdmin()
      except:
          return False
  
  if is_admin():
      progStructure()
  else:
      ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, "", None, 0)
      progStructure()
  </code></pre>
  
  <p>My excel writing function:</p>
  
  <pre><code>def excelWriter(excel_path, data_num, printable_list):
      from time import strftime
      date = strftime("%Y-%m-%d")
      time = strftime("%H:%M:%S")
      if settings("excel_output_subjectNames"):
          patient_name_local = patient_name      
      else:
          patient_name_local = "N/A"
  
      demographic_data = [patient_ID, patient_name_local, patient_admin, date, time, patient_age, patient_sex, patient_edu]
  
      from openpyxl import Workbook
      from openpyxl import load_workbook
  
      wb = Workbook(write_only=False)
  
      try:
          test_name_list = [
                      "(1)MMT", "(2)MOCA", "(3)3MS", "(4)GISD", "(5)ECR",
                      "(6)Sözel Bellek Süreçleri", "(7)Rey Karmaşık Figür", "(8)İz Sürme", "(9)Stroop",
                      "(10)Wisconsin", "(11)Görsel Sözel Test", "(12)Renkli İz Sürme",
                      "(13)Wechsler", "(14)Wechsler-Sayı Dizisi", "(15)Sözel Akıcılık", 
                      "(16)Semantik Akıcılık", "(17)Saat Çizme", "(18)SDOT", "(19)Ayları İleri-Geri Sayma"
                      ]
  
  
          while True:
              try:
                  data_workbook = load_workbook(filename = excel_path + settings("excel_name"), read_only=False)
                  active_sheet = data_workbook.get_sheet_by_name(test_name_list[data_num-1])
                  active_sheet.append(demographic_data + printable_list)
  
                  data_workbook.save(filename = excel_path + settings("excel_name"))
                  break
  
              except:
                  for i in range(len(test_name_list)):
                      wb.create_sheet(title = test_name_list[i])
  
                  wb.save(filename = excel_path + settings("excel_name"))
                  data_workbook = load_workbook(filename = excel_path + settings("excel_name"), read_only=False)
                  active_sheet = data_workbook.get_sheet_by_name("Sheet")
                  data_workbook.remove_sheet(active_sheet)
                  data_workbook.save(filename = excel_path + settings("excel_name"))
                  continue
  
      except:
          raise
  </code></pre>