2017-07-01 217 views
0

這是一個簡單的腳本,用於計算某些神經心理測試的Z分數。Python腳本一直在後臺運行

但最近代碼似乎在退出程序後仍在後臺運行。這個問題在之前並不存在,我使用下面的啓動部分來確保程序在高程下運行,並且顯示的尺寸是正確的。

程序可以使用引發SystemExit的「退出」命令關閉,或者只需按下頂部欄上的X按鈕即可關閉程序。無論如何,程序都在後臺繼續運行。

我哪裏錯了?

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() 

我寫的Excel功能:

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 
+0

您是否收到關機消息? 「程序仍在運行」的症狀究竟是什麼?僅僅是您看到Python可執行文件仍在進程列表中? –

+0

您沒有完整的示例。另外,添加打印語句以查看它卡住的位置。 –

+0

我可以在進程列表中看到python可執行文件,是的。要麼是我的exe文件被凍結,要麼是python本身,如果它只是腳本。我試圖刪除程序後意識到這一點。 –

回答

0

忘了關閉Excel文件寫入後,這看似解決了問題,謝謝大家幫忙。