使用 Python 的 tkinter 将条目字段传输到 excel 文件
Transferring entry fields to excel file using Python's tkinter
我正在尝试创建一个应用程序,要求用户输入收到的新提案的详细信息。然后将条目添加到 excel sheet。我的代码已准备好 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()
您没有在添加条目值后保存您的工作簿。这样做 -
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")
我正在尝试创建一个应用程序,要求用户输入收到的新提案的详细信息。然后将条目添加到 excel sheet。我的代码已准备好 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()
您没有在添加条目值后保存您的工作簿。这样做 -
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")