Question

In: Computer Science

Python 3 Fix this: The program is rewriting the product in excel everytime it gets executed...

Python 3

Fix this: The program is rewriting the product in excel everytime it gets executed i want to make a list and add new products everytime submit button its executed

# 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

# Updated Code

# required library

import tkinter as tk

from tkcalendar import DateEntry

# Using openpyxl because xlswriter can't append rows in an xlsx workbook

from openpyxl import load_workbook

from openpyxl import Workbook

# 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)

# arranging

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')

# Filename in which data is to be written

filename = "dailylog.xlsx"


# callback function

def readValue():

excel_barcode = barcode.get()

excel_product = product.get()

excel_money = money.get()

date = cal._date

print(date)

# Using a try catch block

try:

# If file exists, then load it

wb = load_workbook(filename)

ws = wb.worksheets[0] # select first worksheet

except FileNotFoundError:

# Otherwise create a workbook and add columns

header_row = ["BARCODE", "MONEY", "PRODUCT", "date"]

wb = Workbook()

ws = wb.active

# append the column names

ws.append(header_row)

# Add entries into the new row for inserting into the worksheet

new_row = [excel_barcode, excel_money, excel_product, str(date)]

# Append row by row

ws.append(new_row)

# Save the data in the filename specified

wb.save(filename)

# Clear the date

cleardate()

# 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()

Sample Output :



Related Solutions

Python 3 Fix code everytime i hit submit all the fields in the fomr should be...
Python 3 Fix code everytime i hit submit all the fields in the fomr should be cleaned # 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,...
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...
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...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Python 3 Fix the code so the program reads the file and see if the bar...
Python 3 Fix the code so the program reads the file and see if the bar code was already inputted 3 times if so, it ishows a warning indicating that the item was already tested 3 times 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,...
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale...
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range, it asks him/her to enter a numeric input in the correct range, instead of returning an error. Example: Enter your score: 78 Letter...
Python 3 Fix the code so if the user does not select a value in the...
Python 3 Fix the code so if the user does not select a value in the sold by field, it shows a warning message indicating "Choose one value" import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook 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) #...
Python 3 Fix the code. It is not saving the data into the xls file Code:...
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...
In python write a program that gets a list of integers from input, and outputs non-negative...
In python write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 -7 4 39 -6 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space. Do not end with newline
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT