Skip to main content
 首页 » 编程设计

Python,为什么我的 for 循环创建了两个 docx 文件,而它应该创建一个文件

2025年05月04日13lovecherry

代码:

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的文件。

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

请您参考如下方法:

您需要检查是否有任何选项为 1('否')并根据结果更改文件名。

    no_in_result = any([var.get() == 1 for var in vars]) 
    doc_name = "Failed.docx" if no_in_result else "Test.docx" 
    fn = fn = document.save(doc_name) 
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" 
 
    no_in_result = any([var.get() == 1 for var in vars]) 
    doc_name = "Failed.docx" if no_in_result else "Test.docx" 
    fn = fn = document.save(doc_name) 
    print(fn) 
 
savebtn = Button(root, text = "Save", command = save).grid() 
 
root.mainloop()