Question

In: Computer Science

Python 3 In the field where it says failure day indicate current date if i run...

Python 3

In the field where it says failure day indicate current date if i run the program today it show todays date and if i run the program tomorrow it show me tomorrows date and so on .

The program is rewriting the product everytime it gets executed i want to add new products as a list in excel

# required library
import tkinter as tk
from tkcalendar import DateEntry
import xlsxwriter

# frame
window = tk.Tk()
window.title("daily logs")
#window.resizable(0,0)
# 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="Failed date").grid(row=3, 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)
cal = DateEntry(window, width=12, year=2019, month=6, day=22, background='darkblue', foreground='white', borderwidth=2)
cal.grid(row=3, column=1)
# callback function
def cleardate():
    barcode.delete(0, 'end')
    product.delete(0, 'end')
    money.delete(0, 'end')


# callback function
def readValue():
    excel_barcode = barcode.get()
    excel_product = product.get()
    excel_money = money.get()
    date = cal._date
    print(date)
    workbook = xlsxwriter.Workbook("dailylog.xlsx")
    worksheet = workbook.add_worksheet()
    worksheet.write("A1", "BARCODE")
    worksheet.write("B1", "MONEY")
    worksheet.write("C1", "PRODUCT")
    worksheet.write("D1", "date")
    worksheet.write("A2", excel_barcode)
    worksheet.write("B2", excel_money)
    worksheet.write("C2", excel_product)
    worksheet.write("D2", str(date))
    workbook.close()
    cleardate() #calls cleardate() function

# button to trigger actions
button = tk.Button(text="SUBMIT", command=readValue).grid(row=4, pady=20, padx=20)
button = tk.Button(text="CLEAR", command=cleardate).grid(row=4, column=1, pady=20, padx=20)
window.geometry("500x400")
window.mainloop()

Solutions

Expert Solution

The python module xlsxwriter doesn't allow or have functionality to edit/modify existing excel files.

To add the new product in existing excel file, another python library openpyxl is required. openpyxl provides all the functionality of xlsxwriter and functionality of loading and updating existing excel files.

Updated program imports the openpyxl library (removed xlsxwriter library). if file doesn't exist it creates the file and insert header and record in the excel file. Otherwise it loads the excel file and append a new record in the file.

Complete Program:

# required library
import tkinter as tk
from tkcalendar import DateEntry
# xlsxwriter library doesn't work when update is required in existing excel file
# openpyxl is another library which provide xlswriter functionality and allow to modify existing files
# install openpyxl before executing the program
import openpyxl
import os.path

# frame
window = tk.Tk()
window.title("daily logs")
#window.resizable(0,0)
# 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="Failed date").grid(row=3, 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)

#use current date instead of hardcoded date
cal = DateEntry(window, width=12, background='darkblue', foreground='white', borderwidth=2)

cal.grid(row=3, column=1)
# callback function
def cleardate():
barcode.delete(0, 'end')
product.delete(0, 'end')
money.delete(0, 'end')


# callback function
def readValue():
excel_barcode = barcode.get()
excel_product = product.get()
excel_money = money.get()
date = cal._date

# if file exist then append the new record
if os.path.exists("dailylog.xlsx"):
# load the existing excel file
workbook = openpyxl.load_workbook("dailylog.xlsx")

# get the active worksheet from the loaded workbook
worksheet = workbook.active

# append the record in the excel file and save it
worksheet.append([excel_barcode, excel_money, excel_product, str(date)])
  
# Save the file
workbook.save("dailylog.xlsx")
else:
# file doesn't exist, create it with header and insert the record
workbook = openpyxl.Workbook()

# grab the active worksheet
worksheet = workbook.active

# insert header
worksheet["A1"] = "BARCODE"
worksheet["B1"] = "MONEY"
worksheet["C1"] = "PRODUCT"
worksheet["D1"] = "date"

# insert record
worksheet["A2"] = excel_barcode
worksheet["B2"] = excel_money
worksheet["C2"] = excel_product
worksheet["D2"] = str(date)

# Save the file
workbook.save("dailylog.xlsx")
  
cleardate() #calls cleardate() function

# button to trigger actions
button = tk.Button(text="SUBMIT", command=readValue).grid(row=4, pady=20, padx=20)
button = tk.Button(text="CLEAR", command=cleardate).grid(row=4, column=1, pady=20, padx=20)
window.geometry("500x400")
window.mainloop()

Updated UI:

Updated Excel sheet:


Related Solutions

Python 3 In the field where it says failure day indicate current date if i run...
Python 3 In the field where it says failure day indicate current date if i run the program today it show todays date and if i run the program tomorrow it show me tomorrows date and so on . The program is rewriting the product everytime it gets executed i want to add new products as a list in excel # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs")...
Python 3 Fix this: show the current date where the calendar field is # required library...
Python 3 Fix this: show the current date where the calendar field is # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # 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="Failed date").grid(row=3, 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) cal...
Where it says week 3 stock prices above the chart that I have posted is the...
Where it says week 3 stock prices above the chart that I have posted is the week three prices. These are all the old prices. I need the current ones based off of the information that I have provided from the previous answer. Everything from the top of this page on down to that chart is the previous anwer to the question. Based off of that information, I will need the new question answered at the bottom of the page....
USE PYTHON to find out What day of the week is a given date? i.e. On...
USE PYTHON to find out What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday Write a function is_leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not. .Then, write a function days_since() which takes 3 input integers day, month, year, representing...
Complete the code so that it can convert the date to day of week using python,...
Complete the code so that it can convert the date to day of week using python, the code should pass the doctest def convert_datetime_to_dayofweek(datetime_string): """ This function takes date in the format MON DAY YEAR HH:MM(PM/AM) and returns the day of the week Assume input string is UTC    >>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM') 'Wednesday' >>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM') 'Thursday' """ # code goes here
In python can you fix the error in the line where it says message = input("Input...
In python can you fix the error in the line where it says message = input("Input a lowercase sentence: ") this is the error message I get after running the program and inputing a lowercase sentence Input a lowercase sentence:hello hi MM§MTraceback (most recent call last): MM§M File "client.py", line 14, in <module> MM§M message = input("Input a lowercase sentence:") MM§M File "<string>", line 1 MM§M hello hi MM§M ^ MM§MSyntaxError: unexpected EOF while parsing from socket import * #...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
When I run this, gdb gives me a segmentation fault at the for loop. It says...
When I run this, gdb gives me a segmentation fault at the for loop. It says I'm dereferencing curr with curr->data when curr is null. It doesn't even seem to enter the body of the loop sometimes. I am testing it on a linked list that ends with a strange element. Could you help me get this one done? Thanks so much! Here is the instruction and code: LewList *splitStrange(); This function should split the list in two, adding the...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current date in case it was modified Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox 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="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current...
Python 3 Fix the code so everytime i hit clear the calendar resets to the current date in case it was modified Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox 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="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT