使用pythondocx关闭pythontkintergui的原因不明

2024-09-30 14:38:29 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我运行这个函数时,函数都会被执行,但是程序会停止运行/崩溃,没有错误代码(返回0)。我真的不知道发生了什么,虽然这可能是由于无法关闭docx文件。我很困惑,因为他们没有生成错误代码。

这类问题可能是不允许的。。。不过,我相信它可以提供对所使用的不同模块的见解

def CreateAttendnaceReportScout():
AtScout = AttendanceScout.get()
    AtScout = "".join((char for char in AtScout if char not in string.punctuation))
    print(AtScout)
    Names=AtScout.split()
    GetEventCount="SELECT present FROM eventsscouts WHERE scoutID=(SELECT scoutID FROM scoutinfo WHERE firstname=%s AND secondname=%s)"
    mycursor.execute(GetEventCount,(Names[0],Names[1]))
    myresults=mycursor.fetchall()
    print(myresults)
    NoOfEvents=0
    NoOfN=0
    NoOfY=0
    NoOfPend=0
    for i in myresults:
        NoOfEvents=NoOfEvents+1
        if i[0]==("N"):
            NoOfN=NoOfN+1
        if i[0]==("Y"):
            NoOfY=NoOfY+1
        if i[0]==("Null"):
            NoOfPend=NoOfPend+1

    print(NoOfEvents)
    print(NoOfN)
    print(NoOfY)
    print(NoOfPend)
    #Get List Of Events
    GetEventID = "SELECT eventID,present FROM eventsscouts WHERE scoutID=(SELECT scoutID FROM scoutinfo WHERE firstname=%s AND secondname=%s)"
    mycursor.execute(GetEventID, (Names[0], Names[1]))
    myresults=mycursor.fetchall()
    ListOfEventResponses=[]
    for i in myresults:
        print(i[0])
        GetEventName="SELECT eventname FROM events WHERE eventID=%s"
        mycursortwo.execute(GetEventName,(i[0],))
        EName=mycursortwo.fetchone()
        print(EName[0])
        Var=(i+EName)
        ListOfEventResponses.append(Var)
        print(ListOfEventResponses)



    AtScout="".join((char for char in AtScout if char not in string.punctuation))
    # Defining Labels For Pie Chart
    Labels = ["Present","Absent"]
    # Defining Data Variables For Each Slice Of The Pie Chart
    Slices = [NoOfY,NoOfN]
    #Removing No Value Slices
    if NoOfY==0:
        Slices.remove(NoOfY)
        Labels.remove("Present")
    if NoOfN==0:
        Slices.remove(NoOfN)
        Labels.remove("Absent")
    # Create Pie Chart Of Attendance
    Cols = ['g', 'y', 'c', 'm', 'r', 'b']
    # Plot
    plt.pie(Slices, labels=Labels, colors=Cols,
            autopct='%1.1f%%', shadow=True, startangle=140)

    plt.axis('equal')
    plt.title(AtScout+"'s Percentage Attendance Pie Chart")
    EPieChart = plt.gcf()

    PieChartFileName=AtScout+ " 's AttendancePieChart.png"
    pylab.savefig(PieChartFileName, bbox_inches='tight')
    plt.draw()
    EPieChart.savefig(PieChartFileName, bbox_inches='tight')
    plt.close()







    #Creating Document
    doc = docx.Document()
    run = doc.add_paragraph().add_run()
    # Apply Style
    Tstyle = doc.styles['Normal']
    font = Tstyle.font
    font.name = "Nunito Sans"
    font.size = Pt(48)
    Title = doc.add_paragraph()
    TRun = Title.add_run(AtScout+"'s Attendance Report")
    TRun.bold = True
    doc.add_picture('Scouts_Logo_Stack_Black.png', width=Inches(4.0))
    doc.add_picture(PieChartFileName, width=Inches(4.0))
    paragraph = doc.add_paragraph('')
    #Giving List Of Events And Responses
    ListOfResults= paragraph.add_run("A List Of All Events The Scout Has, Or Has Not Attended Can Be Seen Below: \n")
    ListOfResults.font.name= 'Nunito Sans'
    ListOfResults.font.size= docx.shared.Pt(20)
    for i in ListOfEventResponses:
        if i[1]==("Y"):
            ListOfResults = paragraph.add_run("The Scout Was Present At "+i[2]+"\n")
            ListOfResults.font.name = 'Nunito Sans'
            ListOfResults.font.size = docx.shared.Pt(12)
        if i[1]==("N"):
            ListOfResults = paragraph.add_run("The Scout Was Absent At "+i[2]+"\n")
            ListOfResults.font.name = 'Nunito Sans'
            ListOfResults.font.size = docx.shared.Pt(12)
        if i[1]==("Null"):
            ListOfResults = paragraph.add_run("The Scout Has Signed Up For "+i[2]+"\n")
            ListOfResults.font.name = 'Nunito Sans'
            ListOfResults.font.size = docx.shared.Pt(12)



    Filename=AtScout+"'s Attendance Report"
    doc.save(Filename)

    tk.messagebox.showinfo("Success",AtScout+"'s Attendance Report Completed!")
    raise_frame(AttendanceChoice)

Tags: runinadddocifpltprintfont