In: Computer Science
Python 3
Fix the code. It is not saving the data into the xls file
Code:
import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed date").grid(row=5, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) # arraging barcode.grid(row=0, column=1) product.grid(row=1, column=1) money.grid(row=2, column=1) options = tk.StringVar(window) options.set("Choose one value") # default value soldData = tk.OptionMenu(window, options, "Peter", "John", "Mary", "Jonatan", "Steve") soldData.grid(row=3, column=1) cal = DateEntry(window, width=12, background='darkblue', foreground='white', borderwidth=2) cal.grid(row=5, column=1) #option menu for working product Yes/No optionsWorkingProduct = tk.StringVar(window) optionsWorkingProduct.set("Choose working product") # default value workingProduct = tk.OptionMenu(window, optionsWorkingProduct, "Yes", "No") workingProduct.grid(row=4, column=1) barcode_ls=[] def readValue(): excel_barcode = barcode.get() excel_product = product.get() excel_money = money.get() excel_sold = options.get() excel_workingProduct= optionsWorkingProduct.get() if excel_sold.strip() == 'Choose one value': messagebox.showwarning("Error", "Please select a value for sold by") return if excel_workingProduct.strip() == 'Choose working product': #check for the not selected working product messagebox.showwarning("Error", "Please select a value for working product") return date = datetime.now() print(date) if(barcode_ls.count(excel_barcode)>=3): messagebox.showwarning("Error", "The product is already tested 3 times and it reached the limits") return barcode_ls.append(excel_barcode) data = [excel_barcode, excel_product, excel_money, excel_sold, date, excel_workingProduct] workbook = load_workbook("dailylog.xlsx") worksheet = workbook.worksheets[0] worksheet.append(data) workbook.save("dailylog.xlsx") cleardate() def cleardate(): barcode.delete(0, 'end') product.delete(0, 'end') money.delete(0, 'end') options.set("Choose one value") # default value optionsWorkingProduct.set("Choose working product") # default value set to the working product today = datetime.now() cal.set_date(today) cleardate() # button to trigger actions button = tk.Button(text="SUBMIT", command=readValue).grid(row=6, pady=20, padx=20) button = tk.Button(text="CLEAR", command=cleardate).grid(row=6, column=1, pady=20, padx=20) window.geometry("500x500") window.mainloop()
import tkinter as tk
from tkcalendar import DateEntry
from openpyxl import Workbook #importing Workbook
from tkinter import messagebox
from datetime import datetime
window = tk.Tk()
window.title("daily logs")
window.grid_columnconfigure(1,weight=1)
window.grid_rowconfigure(1,weight=1)
# labels
tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20,
padx=20)
tk.Label(window, text="Products failed").grid(row=1, sticky="W",
pady=20, padx=20)
tk.Label(window, text="Money Lost").grid(row=2, sticky="W",
pady=20, padx=20)
tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20,
padx=20)
tk.Label(window, text="Working product").grid(row=4, sticky="W",
pady=20, padx=20) #Working product label
tk.Label(window, text="Failed date").grid(row=5, sticky="W",
pady=20, padx=20)
# entries
barcode = tk.Entry(window)
product = tk.Entry(window)
money = tk.Entry(window)
# arraging
barcode.grid(row=0, column=1)
product.grid(row=1, column=1)
money.grid(row=2, column=1)
options = tk.StringVar(window)
options.set("Choose one value") # default
value
soldData = tk.OptionMenu(window, options, "Peter", "John", "Mary",
"Jonatan", "Steve")
soldData.grid(row=3, column=1)
cal = DateEntry(window, width=12, background='darkblue',
foreground='white', borderwidth=2)
cal.grid(row=5, column=1)
#option menu for working product Yes/No
optionsWorkingProduct = tk.StringVar(window)
optionsWorkingProduct.set("Choose working
product") # default value
workingProduct = tk.OptionMenu(window, optionsWorkingProduct,
"Yes", "No")
workingProduct.grid(row=4, column=1)
barcode_ls=[]
def readValue():
excel_barcode = barcode.get()
excel_product = product.get()
excel_money = money.get()
excel_sold = options.get()
excel_workingProduct=
optionsWorkingProduct.get()
if excel_sold.strip() == 'Choose one
value':
messagebox.showwarning("Error", "Please select a value for sold
by")
return
if excel_workingProduct.strip() == 'Choose
working product': #check for the not selected working product
messagebox.showwarning("Error", "Please select a value for working
product")
return
date = datetime.now()
print(date)
if(barcode_ls.count(excel_barcode)>=3):
messagebox.showwarning("Error", "The product is already tested 3
times and it reached the limits")
return
barcode_ls.append(excel_barcode)
data = [excel_barcode, excel_product,
excel_money, excel_sold, date, excel_workingProduct]
workbook = Workbook() #creating object
workbook
worksheet = workbook.active
worksheet.append(data) #appending data to
worksheet
workbook.save("dailylog.xlsx") #saving data to
file
def cleardate():
barcode.delete(0, 'end')
product.delete(0, 'end')
money.delete(0, 'end')
options.set("Choose one
value") # default value
optionsWorkingProduct.set("Choose working
product") # default value set to the working product
today = datetime.now()
cal.set_date(today) #removed cleardate
# button to trigger actions
button = tk.Button(text="SUBMIT", command=readValue).grid(row=6,
pady=20, padx=20)
button = tk.Button(text="CLEAR", command=cleardate).grid(row=6,
column=1, pady=20, padx=20)
window.geometry("500x500")
window.mainloop()
If you have any doubt comment me.