如何根据从 tkinter 选择的值保存 docx 文件?

How to save docx file based on values chosen from tkinter?

这是我的代码:

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 = "*"
        elif val == 1:   # checks if no
            cells[2].text = "*"
        elif val == 0:   # checks if N/A
            cells[3].text = "*"

        for x in cells[2].text:
            if "*" in x:
                print("Failed.docx")
            elif "*" not in x:
                print("Test.docx")

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

root.mainloop()

这是我之前的问题:Link

我已将其中一个答案与我的问题结合使用。

我想要实现的目标:

如果 no 已通过单选按钮对任何选项进行 select 编辑,则将文档另存为 failed.docx 如果每个选项都已 select 编辑没有任何 no's 然后将文件保存为 Test.docx.

我的问题是:

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

当我 select 没有选项时 returns 我 Failed.docx。但是如果 no' 的 none 被 selected,它什么都不做? 运行 根本没有 elif 语句。

在您的代码中,您通过在每次迭代中创建 cells 逐行写入 docx。 cells 是包含单个选项的三个单选按钮值的行。假设您为选项 3 选择 Yes,则单元格将包含 [* '' ''] 作为其迭代。

cells[0] = *
cells[1] = ''
cells[2] = ''

此外,当您尝试迭代 cells[1].text 时,您正在尝试迭代一个空项目。这就是为什么它永远不会进入 if-elif 语句,因为它永远不会进入 for 循环。
(不确定我是否设法清楚地解释了这一点,但如果您为未选择的单选按钮或调试器使用一个值,您可以非常清楚地看到发生了什么)

对于一个解决方案,因为你想检查单个列的所有值,你可以使用 table.column.

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 = 'Test.docx'
for cell in table.columns[2].cells[1:4]: #column2 is No column, 1:4 excludes header cell
    if cell.text == '*':
        fn = 'Failed.docx'
        break

print(fn)