首页 > 解决方案 > Python,为什么我的for循环应该创建两个docx文件?

问题描述

代码:

from tkinter import *
from docx import Document

root = Tk()



info = ["Option 1", "Option 2", "Option 3"]


vars = []
for idx,i in enumerate(info):
    var = IntVar(value=0)
    vars.append(var)
    lblOption = Label(root,text=i)
    btnYes = Radiobutton(root, text="Yes", variable=var, value=2)
    btnNo = Radiobutton(root, text="No", variable=var, value=1)
    btnNa = Radiobutton(root, text="N/A", variable=var,value=0)
    lblOption.grid(column=4,row=idx, sticky = W)
    btnYes.grid(column=1,row=idx)
    btnNo.grid(column=2,row=idx)
    btnNa.grid(column=3,row=idx)


def save():
    document = Document()

    #add table
    table = document.add_table(1, 4)
    #style table
    table.style = 'Table Grid'

    #populate header row
    heading_cells = table.rows[0].cells
    heading_cells[0].text = "Options"
    heading_cells[1].text = "Yes"
    heading_cells[2].text = "No"
    heading_cells[3].text = "N/a"

    for idx, item in enumerate(vars):
        cells = table.add_row().cells
        cells[0].text = info[idx]  # gets the option name
        val = item.get()  #radiobutton value
        if val == 2:  # checks if yes
            cells[1].text = "*"
            cells[2].text = "not-selected"
            cells[3].text = "not-selected"
        elif val == 1:   # checks if no
            cells[2].text = "*"
            cells[1].text = "not-selected"
            cells[3].text = "not-selected"
        elif val == 0:   # checks if N/A
            cells[3].text = "*"
            cells[1].text = "not-selected"
            cells[2].text = "not-selected"

        fn = document.save("Test.docx")
        for cell in table.columns[2].cells[1:]:
            if cell.text == '*':
                fn = fn = document.save("failed.docx")
                break

        print(fn)

savebtn = Button(root, text = "Save", command = save).grid()

root.mainloop()

我想要达到的目标:

如果no已通过单选按钮选择了任何选项,则必须将文档另存为Failed.docx. 如果每个选项都被选中而没有任何选项,no's则将文件另存为Test.docx.

我的问题是:

为什么我的最后一个 for 循环和 if 语句不起作用。

如果我no's从单选按钮中选择多个并单击保存,它会生成两个文件Test.docx& Failed.docx,而它应该只创建一个文件Failed.docx

我的目标 我的目标是创建单选按钮,根据选择的选项Yes, no & N/A,它将生成一个docx文件。

例如,如果没有no选择任何一个,则生成一个名为Test.docx.

如果至少no选择了 1 个或多个,则忘记Test.docx并创建一个不同的文件名,称为Failed.docx

标签: pythontkinter

解决方案


这是fn = document.save("Test.docx")每次创建 Test.docx 的那一行。这是一种处理问题的方法:

from tkinter import *
from docx import Document

root = Tk()



info = ["Option 1", "Option 2", "Option 3"]


vars = []
for idx,i in enumerate(info):
    var = IntVar(value=0)
    vars.append(var)
    lblOption = Label(root,text=i)
    btnYes = Radiobutton(root, text="Yes", variable=var, value=2)
    btnNo = Radiobutton(root, text="No", variable=var, value=1)
    btnNa = Radiobutton(root, text="N/A", variable=var,value=0)
    lblOption.grid(column=4,row=idx, sticky = W)
    btnYes.grid(column=1,row=idx)
    btnNo.grid(column=2,row=idx)
    btnNa.grid(column=3,row=idx)


def save():
    document = Document()

    #add table
    table = document.add_table(1, 4)
    #style table
    table.style = 'Table Grid'

    #populate header row
    heading_cells = table.rows[0].cells
    heading_cells[0].text = "Options"
    heading_cells[1].text = "Yes"
    heading_cells[2].text = "No"
    heading_cells[3].text = "N/a"

    yes_flag = True

    for idx, item in enumerate(vars):
        cells = table.add_row().cells
        cells[0].text = info[idx]  # gets the option name
        val = item.get()  #radiobutton value
        if val == 2:  # checks if yes
            cells[1].text = "*"
            cells[2].text = "not-selected"
            cells[3].text = "not-selected"
        elif val == 1:   # checks if no
            cells[2].text = "*"
            cells[1].text = "not-selected"
            cells[3].text = "not-selected"
            yes_flag = False
        elif val == 0:   # checks if N/A
            cells[3].text = "*"
            cells[1].text = "not-selected"
            cells[2].text = "not-selected"
            yes_flag = False

        if yes_flag:
            fn = document.save("Test.docx")
        for cell in table.columns[2].cells[1:]:
            if cell.text == '*':
                fn = fn = document.save("failed.docx")
                break

        print(fn)

savebtn = Button(root, text = "Save", command = save).grid()

root.mainloop()

推荐阅读