繁体   English   中英

当用户写名字和姓氏框时,我希望它使用 tkinter 和 python-docx 在单元格中打印它,我该怎么做?

[英]When the user write the first name and the last name boxs I want it to get print it in cell using tkinter and python-docx, how can I do that?

我有一个使用 docx-python 生成 word 文档的程序,我想使用 tkinter 设计一个 GUI。 那么,当我输入,例如,我想在表格中的 word 文档中打印它的名称时,我该如何为这样的东西编码?

root = Tk()
root.geometry('500x500')
root.title("Registration")

label_1 = Label(root, text="FullName",width=20,font=("bold", 10))
label_1.place(x=80,y=130)
entry_1 = Entry(root)
entry_1.place(x=240,y=130)

root.mainloop()
document = Document()
t = document.add_table(rows=3, cols=2, style='TableGrid')
for row in t.rows:
    row.height = Inches(0.4)
cell = t.cell(0, 0)
cellp=cell.paragraphs[0]
#here where I want the name get print it
cellp.text='Name : '
cellp.add_run(FullName)

首先,您需要向 GUI 添加按钮,因为在root.mainloop()之后没有任何事件将不会执行任何操作。 所以,让我们添加一个按钮:

add_btn = Button(root)
add_btn["text"] = "Add name"
add_btn["command"] = add_to_document
add_btn.place(x=240,y=160)

在上面的示例中,只要单击“添加名称”, add_btn就会调用add_to_document function。

这是您需要拥有文档逻辑的地方。

这是一个带有按钮和 function 的工作示例,该按钮在按下此按钮时打印文本字段的内容。

def add_to_document():
    print("Adding " + entry_1.get())

from tkinter import *
root = Tk()
root.geometry('500x500')
root.title("Registration")

label_1 = Label(root, text="FullName",width=20,font=("bold", 10))
label_1.place(x=80,y=130)
entry_1 = Entry(root)
entry_1.place(x=240,y=130)

# Adding a button
add_btn = Button(root)
add_btn["text"] = "Add name"
add_btn["command"] = add_to_document
add_btn.place(x=240,y=160)

root.mainloop()

还有其他方法可以做到这一点,也有更好的方法来组织事情。 我建议阅读https://docs.python.org/3/library/tkinter.html

他们有一个“hello world”示例,您可以从中汲取灵感。 https://docs.python.org/3/library/tkinter.html#a-simple-hello-world-program

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM