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

Python 如何根据从tkinter中选择的值保存docx文件?,python,tkinter,docx,Python,Tkinter,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

这是我的代码:

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()
这是我先前的问题:

我用其中一个答案把它和我的问题结合起来

我正在努力实现的目标:

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()
如果通过单选按钮为任何选项选择了
no
,则将文档另存为
failed.docx
如果每个选项均未选择任何
no
,则将文件另存为
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"

    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()
为什么我的最后一个for循环和if语句不起作用


当我选择“否”选项时,它返回我Failed.docx。但是如果没有选择任何一个
no'
,它什么也不做?根本不运行elif语句。

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

单元格[0]=*

单元格[1]=''

单元格[2]=''

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

对于解决方案,由于要检查单个列的所有值,因此可以使用


您是否已尝试打印并查看
x
包含哪些选项?@Lafexlos它打印
*
还有
表的功能。列[2]。单元格[1:4]
的功能?@sharksTale
表。列
获取创建的
表中的所有列对象<代码>列[2]
是表中的
列。(0=选项名称,1=是,2=否,3=N/A)命令
表。列[2]。单元格
,获取
列中的所有单元格。第一个单元格(单元格[0])是标题,因此您要检查
单元格[1:4]
。是否可以将
[1:4]
更改为
[1://code>,或者这会有问题?@sharksTale是的,因为您创建的表有4列,
[1://code>和
[1:4]
在这种情况下是相同的。您好,根据从单选按钮中选择的选项,在保存文档方面,我仍然对这段代码有问题