Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am creating a shopping cart in python using tkinter & GUI, but I can't figure out how to code it so that the subtotal

I am creating a shopping cart in python using tkinter & GUI, but I can't figure out how to code it so that the subtotal updates from $0 when quantity is updated. The code is listed below.

from tkinter import * from Inventory import Inventory

expression = 0.00 FG = 'black' BG = 'gray' GUI_BG = 'silver' itemName = [] itemCost = []

def press(num): global expression expression = quantity1 * cost1 equation.set(expression)

if __name__ == "__main__": gui = Tk() gui.configure(background=GUI_BG) gui.title("PyShop - Shopping made fun!") gui.geometry("1020x750") equation = StringVar() inv1 = Inventory() itemName = inv1.invItem itemCost = inv1.invPrice #pull in image for page logo/branding img = PhotoImage(file = "../image/PyShop.png") imgLab = Label(image = img, text="Image Here") imgLab.image = img imgLab.grid(row = 0, column = 0, columnspan = 5, sticky=W+E+N+S, padx=2, pady=2) #column labels colLabel1 = Label(gui, text="Item Name", bg=GUI_BG, font=('', 20)) colLabel1.grid(row = 1, column = 0, sticky=W) colLabel2 = Label(gui, text="Cost", bg=GUI_BG, font=('', 20)) colLabel2.grid(row = 1, column = 1, sticky=E) colLabel3 = Label(gui, text="Quantity", bg=GUI_BG, font=('', 20), width=10) colLabel3.grid(row = 1, column = 2) colLabel4 = Label(gui, text="Click to Add to Cart", bg=GUI_BG, font=('', 20), width=15) colLabel4.grid(row = 1, column = 3) colLabel5 = Label(gui, text="Item Subtotal", bg=GUI_BG, font=('', 20)) colLabel5.grid(row = 1, column = 4,sticky=E) #Item Name variables item1 = itemName[0] item2 = itemName[1] item3 = itemName[2] item4 = itemName[3] item5 = itemName[4] item6 = itemName[5] item7 = itemName[6] item8 = itemName[7] #Item Name fields itemLabel1 = Label(gui, text=item1, bg=GUI_BG, font=('', 15)) itemLabel1.grid(row = 2, column = 0, sticky=W) itemLabel2 = Label(gui, text=item2, bg=GUI_BG, font=('', 15)) itemLabel2.grid(row = 3, column = 0, sticky=W) itemLabel3 = Label(gui, text=item3, bg=GUI_BG, font=('', 15)) itemLabel3.grid(row = 4, column = 0, sticky=W) itemLabel4 = Label(gui, text=item4, bg=GUI_BG, font=('', 15)) itemLabel4.grid(row = 5, column = 0, sticky=W) itemLabel5 = Label(gui, text=item5, bg=GUI_BG, font=('', 15)) itemLabel5.grid(row = 6, column = 0, sticky=W) itemLabel6 = Label(gui, text=item5, bg=GUI_BG, font=('', 15)) itemLabel6.grid(row = 7, column = 0, sticky=W) itemLabel7 = Label(gui, text=item5, bg=GUI_BG, font=('', 15)) itemLabel7.grid(row = 8, column = 0, sticky=W) itemLabel8 = Label(gui, text=item5, bg=GUI_BG, font=('', 15)) itemLabel8.grid(row = 9, column = 0, sticky=W) #Item cost variables cost1 = itemCost[0] cost2 = itemCost[1] cost3 = itemCost[2] cost4 = itemCost[3] cost5 = itemCost[4] cost6 = itemCost[5] cost7 = itemCost[6] cost8 = itemCost[7] #Item cost fields costLabel1 = Label(gui, text=("$%.2f"% cost1), bg=GUI_BG, font=('', 15)) costLabel1.grid(row = 2, column = 1, sticky=E) costLabel2 = Label(gui, text=("$%.2f"% cost2), bg=GUI_BG, font=('', 15)) costLabel2.grid(row = 3, column = 1, sticky=E) costLabel3 = Label(gui, text=("$%.2f"% cost3), bg=GUI_BG, font=('', 15)) costLabel3.grid(row = 4, column = 1, sticky=E) costLabel4 = Label(gui, text=("$%.2f"% cost4), bg=GUI_BG, font=('', 15)) costLabel4.grid(row = 5, column = 1, sticky=E) costLabel5 = Label(gui, text=("$%.2f"% cost5), bg=GUI_BG, font=('', 15)) costLabel5.grid(row = 6, column = 1, sticky=E) costLabel6 = Label(gui, text=("$%.2f"% cost6), bg=GUI_BG, font=('', 15)) costLabel6.grid(row = 7, column = 1, sticky=E) costLabel7 = Label(gui, text=("$%.2f"% cost7), bg=GUI_BG, font=('', 15)) costLabel7.grid(row = 8, column = 1, sticky=E) costLabel8 = Label(gui, text=("$%.2f"% cost8), bg=GUI_BG, font=('', 15)) costLabel8.grid(row = 9, column = 1, sticky=E) #Quantity variables quantity1 = IntVar() quantity2 = IntVar() quantity3 = IntVar() quantity4 = IntVar() quantity5 = IntVar() quantity6 = IntVar() quantity7 = IntVar() quantity8 = IntVar() #Quantity fields quantity_field1 = Entry(gui, textvariable=quantity1, font=('', 15), width='5') quantity_field1.grid(row = 2, column = 2) quantity_field2 = Entry(gui, textvariable=quantity2, font=('', 15), width='5') quantity_field2.grid(row = 3, column = 2) quantity_field3 = Entry(gui, textvariable=quantity3, font=('', 15), width='5') quantity_field3.grid(row = 4, column = 2) quantity_field4 = Entry(gui, textvariable=quantity4, font=('', 15), width='5') quantity_field4.grid(row = 5, column = 2) quantity_field5 = Entry(gui, textvariable=quantity5, font=('', 15), width='5') quantity_field5.grid(row = 6, column = 2) quantity_field6 = Entry(gui, textvariable=quantity6, font=('', 15), width='5') quantity_field6.grid(row = 7, column = 2) quantity_field7 = Entry(gui, textvariable=quantity7, font=('', 15), width='5') quantity_field7.grid(row = 8, column = 2) quantity_field8 = Entry(gui, textvariable=quantity8, font=('', 15), width='5') quantity_field8.grid(row = 9, column = 2) #Command buttons ************* add command to add to cart list ******** , command = self._addCart addBtn1 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn1.grid(row = 2, column = 3) addBtn2 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn2.grid(row = 3, column = 3) addBtn3 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn3.grid(row = 4, column = 3) addBtn4 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn4.grid(row = 5, column = 3) addBtn5 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn5.grid(row = 6, column = 3) addBtn6 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn6.grid(row = 7, column = 3) addBtn7 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn7.grid(row = 8, column = 3) addBtn8 = Button(gui, text = "Add to Cart", fg=FG, bg=BG, font=('', 12)) addBtn8.grid(row = 9, column = 3) spacer = Label(gui, text=" ", bg=GUI_BG, height='3') spacer.grid(row = 10, column = 2) #Discounts discVar = StringVar() discLabel = Label(gui, text="Enter Discount Code: ", bg=GUI_BG, font=('', 20)) discLabel.grid(row = 11, column = 1) discField = Entry(gui, textvariable=discVar, font=('', 15), width='10') discField.grid(row = 11, column = 2) #Total total= StringVar() totalLabel = Label(gui, text="TOTAL: ", bg=GUI_BG, font=('', 20)) totalLabel.grid(row = 11, column = 3) #totalField = Entry(gui, textvariable=total, font=('', 15), width='5') *****Need total calculation, not Entry #totalField.grid(row = 11, column = 4) # start the GUIold gui.mainloop()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions