python - 当用户编写名字和姓氏框时,我希望它使用 tkinter 和 python-docx 在单元格中打印它,我该怎么做?
问题描述
我有一个使用 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
这是您需要拥有文档逻辑的地方。
这是一个工作示例,其中包含一个按钮和一个在按下此按钮时打印文本字段内容的函数。
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
推荐阅读
- zk - 为什么/何时 Executions.getCurrent() 为空?- 如何调试它?
- angular - 如何从具有特定分钟间隔的特定小时开始创建小时数组
- javascript - 忽略 JSON 属性并仅获取值
- reactjs - 函数缺少返回类型 - 在反应(打字稿)代码中
- python - 如何将 zodb 对象日期时间转换为 Python?
- c# - C# Google Analytics api 调用错误的 SSIS 执行进程任务说操作系统的应用程序无效
- python - 当您具有相同的值时如何拆分列?
- java - 'BoxLayout 不能与 .add 共享'
- excel - 如何查看哪些 A 列条目未出现在 B 列中?
- java - 是否可以在运行时以编程方式编辑 Wildfly 服务器的standalon.xml 文件以更改数据库设置?