Struggling with making this code work in my GUI. I am overloaded with errors and trying to understand syntactically how I can make this work. I am trying to make an order form that takes the number of cookies a customer wants and computes their total. I know that this certainly has to be some simple thing I'm overlooking.
from tkinter import Button, Entry, Label,Tk
from tkinter.messagebox import showinfo
class CookieOrderForm(Tk):
def __init__(self,parent=None):
Tk.__init__(self, parent)
self.title("Cookie Form")
self.make_widgets()
def total(self):
order = sum(int(e.get()) for e in (self.chipEn, self.oatEn, self.oreoEn))
txt = ("Your Order Total Is: " str(order))
self.new.config(text = txt)
def make_widgets(self):
chip = Label(self, text='Chocolate Chip ($1.00)').grid(row=1, column=0)
self.chipEn = Entry(self)
self.chipEn.grid(row=1, column=1, columnspan=4)
oat = Label(self, text='Oatmeal ($1.50)').grid(row=2, column=0)
self.oatEn = Entry(self)
self.oatEn.grid(row=2, column=1, columnspan=4)
Button(self,text='Order Total', command=lambda:self.total()).grid(row=4, column=2)
oreo = Label(self, text='Oreo ($2.00)').grid(row=3, column=0)
self.oreoEn = Entry(self)
self.oreoEn.grid(row=3, column=1, columnspan=4)
self.new = Label(self)
self.new.grid(row=3, column=1)
CookieOrderForm().mainloop()
I think my main issue is coming from this line here
order = sum(int(e.get()) for e in (self.chipEn, self.oatEn, self.oreoEn))
EDIT:::: Error Messages Included
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\17734\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "C:\Users\17734\OneDrive\Desktop\fall 2021\csc242\homework\CSC242hw4.py", line 91, in <lambda>
Button(self,text='Order Total', command=lambda:self.total()).grid(row=4, column=2)
File "C:\Users\17734\OneDrive\Desktop\fall 2021\csc242\homework\CSC242hw4.py", line 78, in total
order = sum(int(e.get()) for e in (self.chipEn, self.oatEn, self.oreoEn))
File "C:\Users\17734\OneDrive\Desktop\fall 2021\csc242\homework\CSC242hw4.py", line 78, in <genexpr>
order = sum(int(e.get()) for e in (self.chipEn, self.oatEn, self.oreoEn))
ValueError: invalid literal for int() with base 10: ''
CodePudding user response:
You may get this error only if you have empty Entry
when you press Button
.
Try int("")
and you see the same error.
You may get similar error when you put some text (like Hello World
) instead number.
You could put sum()
in try/except
to catch errors.
def total(self):
try:
order = sum(int(e.get()) for e in (self.chipEn, self.oatEn, self.oreoEn))
txt = ("Your Order Total Is: " str(order))
except Exception as ex:
print('Exception:', ex)
txt = 'Your have wrong values in Order'
self.new.config(text = txt)
Eventually you should convert every value separatelly in separated try/catch
to see which Entry
has wrong value and display message with better information.
CodePudding user response:
This may not be the best solution but it works.
Added function to check if your input is a digit, if not it will return 0
if yes it will return int
from tkinter import Button, DoubleVar, Entry, Label, StringVar,Tk
from tkinter.messagebox import showinfo
class CookieOrderForm(Tk):
def __init__(self,parent=None):
Tk.__init__(self, parent)
self.title("Cookie Form")
self.make_widgets()
def entryIsDigit(self, number: str) -> int:
if number.isdigit() == True:
return int(number)
return 0
def total(self):
order = sum(e for e in (self.entryIsDigit(self.chipEn.get()), self.entryIsDigit(self.oatEn.get()), self.entryIsDigit(self.oreoEn.get())))
txt = ("Your Order Total Is: " str(order))
self.new.config(text = txt)
def make_widgets(self):
chip = Label(self, text='Chocolate Chip ($1.00)').grid(row=1, column=0)
self.chipEn = Entry(self)
self.chipEn.grid(row=1, column=1, columnspan=4)
oat = Label(self, text='Oatmeal ($1.50)').grid(row=2, column=0)
self.oatEn = Entry(self)
self.oatEn.grid(row=2, column=1, columnspan=4)
Button(self,text='Order Total', command=lambda:self.total()).grid(row=4, column=2)
oreo = Label(self, text='Oreo ($2.00)').grid(row=3, column=0)
self.oreoEn = Entry(self)
self.oreoEn.grid(row=3, column=1, columnspan=4)
self.new = Label(self)
self.new.grid(row=3, column=1)
CookieOrderForm().mainloop()```