【问题标题】:Transferring entry fields to excel file using Python's tkinter使用 Python 的 tkinter 将输入字段传输到 excel 文件
【发布时间】:2020-07-08 09:05:48
【问题描述】:

我正在尝试创建一个应用程序,要求用户输入收到的新提案的详细信息。然后将条目添加到 Excel 工作表中。我的代码已准备好 excel 文件,并且我添加了所有必需的标签和输入字段,但在将输入字段的内容传输到 excel 文件时遇到问题。

我认为我的主要问题在于new_entry 函数。我没有收到任何错误,只是代码没有按预期工作。

我的代码如下

from openpyxl import *
import tkinter as tk
# 1 is for client name
# 2 is for service type
# 3 is for Deadline date
# 4 is for responsible party
def new_entry():
    new_line = sheet.max_row + 1
    sheet.cell(column=1, row=new_line, value=txtfld_1.get())
    sheet.cell(column=2, row=new_line, value=txtfld_2.get())
    sheet.cell(column=3, row=new_line, value=txtfld_3.get())
    sheet.cell(column=4, row=new_line, value=txtfld_4.get())

def close_window():
    window.destroy()


try:
    workbook = load_workbook(filename="Received Proposals.xlsx")
    sheet = workbook.active
except:
    workbook = Workbook()
    sheet = workbook.active

    sheet["A1"] = "Client Name"
    sheet["B1"] = "Required Service"
    sheet["C1"] = "Client Deadline"
    sheet["D1"] = "Responsible Consultant"

window = tk.Tk()

window.title('New Proposal Window')
window.geometry("500x200")

btn_exit = tk.Button(window, text="Exit", fg='black', command = close_window)
btn_exit.place(x=400, y=160)

lbl_0 = tk.Label(window, text="New Proposal Entry", fg='black', font=("Helvetica", 8))
lbl_0.place(x=200, y=15)

lbl_1 = tk.Label(window, text="Client Name", fg='black', font=("Helvetica", 8))
lbl_1.place(x=20, y=50)

lbl_2 = tk.Label(window, text="Required Service", fg='black', font=("Helvetica", 8))
lbl_2.place(x=20, y=75)

lbl_3 = tk.Label(window, text="Client Deadline", fg='black', font=("Helvetica", 8))
lbl_3.place(x=20, y=100)

lbl_4 = tk.Label(window, text="Responsible Consultant", fg='black', font=("Helvetica", 8))
lbl_4.place(x=20, y=125)

txtfld_1 = tk.Entry(window, bg='white',fg='black', bd=5)
txtfld_1.place(x=350, y=50)

txtfld_2 = tk.Entry(window, bg='white', fg='black', bd=5)
txtfld_2.place(x=350, y=75)

txtfld_3 = tk.Entry(window, bg='white', fg='black', bd=5)
txtfld_3.place(x=350, y=100)

txtfld_4 = tk.Entry(window, bg='white', fg='black', bd=5)
txtfld_4.place(x=350, y=125)

btn = tk.Button(window, text="Save Entry", fg='black', command = new_entry)
btn.place(x=300, y=160)

workbook.save(filename="Received Proposals.xlsx")

window.mainloop()

【问题讨论】:

  • 您应该将更改保存在 new_entry() 中。

标签: tkinter tkinter-entry python-3.8


【解决方案1】:

添加条目值后,您没有保存工作簿。这样做 -

def new_entry():
    new_line = sheet.max_row + 1
    sheet.cell(column=1, row=new_line, value=txtfld_1.get())
    sheet.cell(column=2, row=new_line, value=txtfld_2.get())
    sheet.cell(column=3, row=new_line, value=txtfld_3.get())
    sheet.cell(column=4, row=new_line, value=txtfld_4.get())
    workbook.save(filename="Received Proposals.xlsx")

【讨论】:

  • 非常感谢!!!将“保存”添加到函数定义中效果很好
猜你喜欢
  • 2020-02-23
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 2017-03-30
  • 2014-03-20
  • 1970-01-01
相关资源
最近更新 更多