Python脚本在后台继续运行

时间:2017-07-01 19:23:05

标签: python ctypes

这是一个简单的脚本,用于计算一些神经心理学测试的Z分数。

但是最近代码似乎在退出程序后仍然在后台运行。这个问题以前不存在,我使用下面的启动文件来确保程序以高程运行,并且显示的大小正确。

可以使用"退出"关闭程序。只需按顶部栏上的X按钮即可引发SystemExit的命令。无论如何,程序都会在后台运行。

我哪里出错了?

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

1 个答案:

答案 0 :(得分:0)

写完后忘记关闭excel文件,这似乎解决了问题,谢谢大家帮帮忙。