Question

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)

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

def readValue():
    excel_barcode = barcode.get()
    excel_product = product.get()
    excel_money = money.get()
    excel_sold = options.get()
    if excel_sold.strip() == 'Choose one value':
        messagebox.showwarning("Error", "Please select a value for sold by")
        return
    date = datetime.now()
    print(date)
    data = [excel_barcode, excel_product, excel_money, excel_sold, date]
    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
    today = datetime.now()
    cal.set_date(today)


# button to trigger actions
button = tk.Button(text="SUBMIT", command=readValue).grid(row=5, pady=20, padx=20)
button = tk.Button(text="CLEAR", command=cleardate).grid(row=5, column=1, pady=20, padx=20)
window.geometry("500x400")
window.mainloop()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The python after doing the required changes asked in question is as follows:

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

#creating list for barcode
barcode_ls=[]
def readValue():
excel_barcode = barcode.get()
excel_product = product.get()
excel_money = money.get()
excel_sold = options.get()
if excel_sold.strip() == 'Choose one value':
messagebox.showwarning("Error", "Please select a value for sold by")
return
date = datetime.now()
if(barcode_ls.count(excel_barcode)>=3):
messagebox.showwarning("Error", "The product is already tested 3 times and it reached the limits")
return
else:
barcode_ls.append(excel_barcode)
print(date)
data = [excel_barcode, excel_product, excel_money, excel_sold, date]
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
today = datetime.now()
cal.set_date(today)


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

ouput:

Explanation:

Here I have added a list called barcode_ls when the program is run this list will save all the barcode values

while entering the values the barcode values are checked in the list if it is present three times and again if they are adding the same barcode value then it will raise the error as asked in the question.

I hope you got the answer and understand it. If any question asks in comments I will help in it.

Thank you:):)

Add a comment
Know the answer?
Add Answer to:
Python 3 Fix the code so if the user enter the same bar code more than...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3 Code does not save the data into excel properly ​​​​​​​ # required library import...

    Python 3 Code does not save the data into excel properly ​​​​​​​ # 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 =...

  • Python 3 How to change the icon in the error message want to put a yellow...

    Python 3 How to change the icon in the error message want to put a yellow warning icon 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 barcode = tk.Entry(window) product =...

  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • Demonstrate this result for the parallel calculation. CODE: from Tkinter import * # MODIFY THIS TO...

    Demonstrate this result for the parallel calculation. CODE: from Tkinter import * # MODIFY THIS TO HANDLE THREE RESISTORS __author__ = "robincarr" """ GUI version of Resistor Calculator finds the equivalent resistance for two resistors connected either in series or parallel. Features entry widgets and buttons.""" def series_calculation(): r1 = float(resistor1.get()) r2 = float(resistor2.get()) req = r1 + r2 equivalent_resistance.delete(0, END) # Clear the previous result. equivalent_resistance.insert(0, req) print("Resistor 1: %s\nResistor 2: %s" % (r1, r2)) print "The equivalent series...

  • Help with Python code. Right now I'm creating a code in Python to create a GUI....

    Help with Python code. Right now I'm creating a code in Python to create a GUI. Here's my code: #All modules are built into Python so there is no need for any installation import tkinter from tkinter import Label from tkinter import Entry def calculatewages(): hours=float(nhours.get()) nsal=float(nwage.get()) wage=nsal*hours labelresult=Label(myGUI,text="Weekly Pay: $ %.2f" % wage).grid(row=7,column=2) return Tk = tkinter.Tk()    myGUI=Tk myGUI.geometry('400x200+100+200') myGUI.title('Pay Calculator') nwage=float() nhours=float() label1=Label(myGUI,text='Enter the number of hours worked for the week').grid(row=1, column=0) label2=Label(myGUI,text='Enter the pay rate').grid(row=2, column=0)...

  • " In the workshop exercises you have used Python's Tkinter module to create simple Graphical User Interfaces. The following code is an incomplete Python program relying on Tkinter (with delib...

    " In the workshop exercises you have used Python's Tkinter module to create simple Graphical User Interfaces. The following code is an incomplete Python program relying on Tkinter (with deliberately unhelpful variable and function names) which you must complete by filling in the blanks. When complete the program should create a window with two buttons, labelled and!!', respectively. When the button la belled'???' is pushed nothing happens. When the button labelled'!!"is pushed both buttons' labels are set to'!!!' from tkinter...

  • Im posting a python program and need to convert it to C++ language. import Tkinter import...

    Im posting a python program and need to convert it to C++ language. import Tkinter import Tkinter as tk from Tkinter import * import time def current_iso8601():     """Get current date and time in ISO8601"""     # https://en.wikipedia.org/wiki/ISO_8601     # https://xkcd.com/1179/     return time.strftime("%m.%d.%Y DATE %H:%M:%S TIME") class Application(tk.Frame):     def __init__(self, master=None):                 tk.Frame.__init__(self, master)         self.pack()         self.createWidgets()         def tstampIn(self):                 clockFile = open("clockTracker.txt","w")         print "You have successfuly Clocked In \n"         clockIn...

  • Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste...

    Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste version): from tkinter import Tk, Label, Entry, Button from random import * class Craps(Tk): #set up the main window def __init__(self, parent=None): Tk.__init__(self, parent) self.title('Play Craps') self.new_game() self.make_widgets() #when a new game is started, the firstRoll will start at 0 def new_game(self): self.firstRoll = 0    #create and place the widgets in the window def make_widgets(self): Label(self, text="Die 1").grid(row=0, column=0, columnspan=1) Label(self, text="Die 2").grid(row=0,...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT