Question

In: Computer Science

Python 3 Fix the code so if the user enter the same bar code more than...

Python 3

Fix the code so if the user enter the same bar code more than three times, it shows a warning message indicating that the product was already tested 3 times and it reached the limits

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="Failed date").grid(row=4, sticky="W", pady=20, padx=20)
tk.Label(window, text="Working product").grid(row=5, sticky="W", pady=20, padx=20)  # Working product label

# 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=4, 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=5, column=1)
barcode_ls = []


def readValue():
    count = 0
    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
    calendar_date = datetime.strptime(cal.get(), "%m/%d/%y")
    time_now = datetime.now()
    if calendar_date > time_now:
        messagebox.showwarning("Error", "Please select a correct date")
        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)
    data = [excel_barcode, excel_product, excel_money, excel_sold, date, excel_workingProduct]
    workbook = load_workbook("dailylog.xlsx")
    worksheet = workbook.worksheets[0]
    # Added a small snippet of code
    for cell in worksheet["C"]:  # checks every last filled cell
        if cell.value == excel_barcode:  # if the value of the cell equals to the excel_barcode
            count += 1  # count increases
    if count == 3:
        messagebox.showwarning("Error", "The product was already tested 3 times and it reached the limits")
        return
    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)


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

Solutions

Expert Solution

Hi,

Your referring column C in the code( for cell in worksheet["C"])

It should be column A,not column C

please find changed code image and sample output image below

Python 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="Failed date").grid(row=4, sticky="W", pady=20, padx=20)
tk.Label(window, text="Working product").grid(row=5, sticky="W", pady=20, padx=20) # Working product label

# 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=4, 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=5, column=1)
barcode_ls = []


def readValue():
count = 0
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
calendar_date = datetime.strptime(cal.get(), "%m/%d/%y")
time_now = datetime.now()
if calendar_date > time_now:
messagebox.showwarning("Error", "Please select a correct date")
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)
data = [excel_barcode, excel_product, excel_money, excel_sold, date, excel_workingProduct]
workbook = load_workbook("dailylog.xlsx")
worksheet = workbook.worksheets[0]
# Added a small snippet of code
for cell in worksheet["A"]: # checks every last filled cell
if cell.value == excel_barcode: # if the value of the cell equals to the excel_barcode
count += 1 # count increases
if count >= 3:
messagebox.showwarning("Error", "The product was already tested 3 times and it reached the limits")
return
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)


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

Python Code as Image:

Sample Output:


Related Solutions

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 so i can make the window larger or smaller and the...
Python 3 Fix the code so i can make the window larger or smaller and the fields adjusts everytime according to the window size import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook 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="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 barcode = tk.Entry(window)...
Python 3 Fix the code so i can make the window larger and the fields increase...
Python 3 Fix the code so i can make the window larger and the fields increase size import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook 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="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 barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) #...
Python 3 Fix the code so i can make the window larger and the fields increase...
Python 3 Fix the code so i can make the window larger and the fields increase size Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook 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="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 barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window)...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
1. Write Python code to - (a) ask user to enter a list of animals, one...
1. Write Python code to - (a) ask user to enter a list of animals, one item at a time, until “done” to terminate input (b) randomly display a selected item from the list, starting with “I like ______”. When run, the output should look something like this: Enter an item; 'done' when finished: cats Enter an item; 'done' when finished: dogs Enter an item; 'done' when finished: guinea pigs Enter an item; 'done' when finished: done You are done...
Python 3 Fix the code and rovide the correct indentation Code: import tkinter as tk from...
Python 3 Fix the code and rovide the correct indentation 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="Failed date").grid(row=4, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money =...
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter...
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter 5 test scores. The program will display a letter grade for each test score and an average grade for the test scores entered. The program will write the student name and average test score to a text file (“studentgrades.txt”). Three functions are needed for this program. def letter_grade( test_score) Test Score Letter Grade 90-100 A 80-89 B 70-79 C 60-69 D Below 60 F...
Please write in Python code please: Write a program that asks the user to enter 5...
Please write in Python code please: Write a program that asks the user to enter 5 test scores between 0 and 100. The program should display a letter grade for each score and the average test score. You will need to write the following functions, including main: calc_average – The function should accept a list of 5 test scores as an input argument, and return the average of the scores determine_grade – The function should accept a test score as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT