''' Kyle Willett ITMD 513 HW 12 - GUI - Main program file Purpose: Models a change calculator and handles most if not all error cases. Graphical input used. ''' from tkinter import * import tkinter.messagebox window = Tk() window.title("Change Counter") window.geometry('600x400') window.configure(background = "light yellow"); instructions = Label(window, text="Enter the number of each coin type and hit, compute:") instructions.grid(column = 1, row = 0) #Each coin is handled, first a label for coin name, a text entry box, a label for value, and a value box. quarters_label = Label(window ,text = "Quarters:").grid(row = 1,column = 0) quarters_entry_box = Entry(window) quarters_entry_box.grid(row = 1,column = 1) quarters_label2 = Label(window, text="Quarter Value: $").grid(row = 1, column = 2) quarters_value = Label(window, text="0.00", borderwidth=2, relief="solid") quarters_value.grid(row=1, column = 3) dimes_label = Label(window ,text = "Dimes:").grid(row = 2,column = 0) dimes_entry_box = Entry(window) dimes_entry_box.grid(row = 2,column = 1) dimes_label2 = Label(window, text="Dime Value: $").grid(row = 2, column = 2) dimes_value = Label(window, text="0.00", borderwidth=2, relief="solid") dimes_value.grid(row=2, column = 3) Nickels_label = Label(window ,text = "Nickels:").grid(row = 3,column = 0) Nickels_entry_box = Entry(window) Nickels_entry_box.grid(row = 3,column = 1) Nickles_label2 = Label(window, text="Nickel Value: $").grid(row = 3, column = 2) Nickles_value = Label(window, text="0.00", borderwidth=2, relief="solid") Nickles_value.grid(row=3, column = 3) Pennies_label = Label(window ,text = "Pennies:").grid(row = 4,column = 0) Pennies_entry_box = Entry(window) Pennies_entry_box.grid(row = 4,column = 1) Pennies_label2 = Label(window, text="Pennies Value: $").grid(row = 4, column = 2) Pennies_value = Label(window, text="0.00", borderwidth=2, relief="solid") Pennies_value.grid(row=4, column = 3) half_dollars_label = Label(window ,text = "Half Dollars:").grid(row = 5,column = 0) half_dollars_entry_box = Entry(window) half_dollars_entry_box.grid(row = 5,column = 1) half_dollars_label2 = Label(window, text="Half Dollar Value: $").grid(row = 5, column = 2) half_dollars_value = Label(window, text="0.00", borderwidth=2, relief="solid") half_dollars_value.grid(row=5, column = 3) dollar_coins_label = Label(window ,text = "Dollar Coins:").grid(row = 6,column = 0) dollar_coins_entry_box = Entry(window) dollar_coins_entry_box.grid(row = 6,column = 1) dollar_coins_label2 = Label(window, text="Dollar coin Value: $").grid(row = 6, column = 2) dollar_coins_value = Label(window, text="0.00", borderwidth=2, relief="solid") dollar_coins_value.grid(row=6, column = 3) #Total label and compute button total_label = Label(window, text="Total Change value: $").grid(row=7, column = 2) total_value_label = Label(window, text="0.00", borderwidth=4, relief="solid") total_value_label.grid(row=7, column = 3) #calculate Function def calculate(): #Check for empty boxes, abort if any found #source on how to do: https://stackoverflow.com/questions/15455113/tkinter-check-if-entry-box-is-empty if len(quarters_entry_box.get()) == 0: tkinter.messagebox.showwarning("Warning", "A text field is blank, please enter 0 for all blank text fields") return if len(dimes_entry_box.get()) == 0: tkinter.messagebox.showwarning("Warning", "A text field is blank, please enter 0 for all blank text fields") return if len(Nickels_entry_box.get()) == 0: tkinter.messagebox.showwarning("Warning", "A text field is blank, please enter 0 for all blank text fields") return if len(Pennies_entry_box.get()) == 0: tkinter.messagebox.showwarning("Warning", "A text field is blank, please enter 0 for all blank text fields") return if len(half_dollars_entry_box.get()) == 0: tkinter.messagebox.showwarning("Warning", "A text field is blank, please enter 0 for all blank text fields") return if len(dollar_coins_entry_box.get()) == 0: tkinter.messagebox.showwarning("Warning", "A text field is blank, please enter 0 for all blank text fields") return #Check if integer values, abort if non integer values found #Source on how to do https://docs.python.org/3.7/tutorial/errors.html try: quarter_val = int(quarters_entry_box.get()) except ValueError: tkinter.messagebox.showwarning("Warning", "Integer data type expected") return try: dime_val = int(dimes_entry_box.get()) except ValueError: tkinter.messagebox.showwarning("Warning", "Integer data type expected") return try: nickel_val = int(Nickels_entry_box.get()) except ValueError: tkinter.messagebox.showwarning("Warning", "Integer data type expected") return try: pennies_val = int(Pennies_entry_box.get()) except ValueError: tkinter.messagebox.showwarning("Warning", "Integer data type expected") return try: half_dollar_val = int(half_dollars_entry_box.get()) except ValueError: tkinter.messagebox.showwarning("Warning", "Integer data type expected") return try: dollar_coin_val = int(dollar_coins_entry_box.get()) except ValueError: tkinter.messagebox.showwarning("Warning", "Integer data type expected") return #Check if values are negative and abort if any are. if quarter_val < 0: tkinter.messagebox.showwarning("Warning", "Values must be positive integers") return if dime_val < 0: tkinter.messagebox.showwarning("Warning", "Values must be positive integers") return if nickel_val < 0: tkinter.messagebox.showwarning("Warning", "Values must be positive integers") return if pennies_val < 0: tkinter.messagebox.showwarning("Warning", "Values must be positive integers") return if half_dollar_val < 0: tkinter.messagebox.showwarning("Warning", "Values must be positive integers") return if dollar_coin_val < 0: tkinter.messagebox.showwarning("Warning", "Values must be positive integers") return quarterAmount = quarter_val * 0.25 dimeAmmount = dime_val * 0.10 nickelAmmount = nickel_val * 0.05 penniesAmmount = pennies_val * 0.01 halfDollarAmmount = half_dollar_val * 0.5 dollarCoinAmmount = dollar_coin_val * 1 quarters_value.config(text=quarterAmount) dimes_value.config(text=dimeAmmount) Nickles_value.config(text=nickelAmmount) Pennies_value.config(text=penniesAmmount) half_dollars_value.config(text=halfDollarAmmount) dollar_coins_value.config(text=dollarCoinAmmount) totalSum = quarterAmount + dimeAmmount + nickelAmmount + penniesAmmount + halfDollarAmmount + dollarCoinAmmount total_value_label.config(text=totalSum) btn = Button(window, text="Compute", command=calculate, borderwidth=4, relief="solid").grid(row=7, column= 1) window.mainloop()