Search code examples
pythontkintercombobox

Referencing Combo Box Values in Tkinter


I am trying to write a Python code to basically do a sumproduct function based on the item selected and the quantity of that item selected.

My code is below. I am having trouble referencing the combobox values. The calculate function is where I'm going wrong. How do I reference the comboboxes I have inputted onto the 'NewWindow'? I add comboboxes to the page based on the number of items selected and the all have the same values,etc.

For example if I select 2 'pizzas' and 1 'CocaCola' then the code should print 33. ((2*$15)+(1*$3))

This is my error:

File "C:\Users\aaaaa\Documents\pizzamenu.py", line 41, in calculate cost = fooditems[x] KeyError: 0

>


fooditems = {'pizza' : [15] , 'breadsticks' : [5] ,'wings' : [10],'CocaCola' : [3] ,'brownie' : [2]}
fooditems2 = []
quantity = ['1','2','3','4']



import tkinter as tk
from tkinter import *
from tkinter import ttk

menu = Tk()
menu.geometry('500x300')

check_boxes = {item:tk.IntVar() for item in fooditems}

for item in fooditems:
    cb = tk.Checkbutton(menu, text=item, variable=check_boxes[item], anchor='w', onvalue=1, offvalue=0, width=50)
    cb.pack()

combobox = ttk.Combobox(menu, values=quantity)


def Open():
    New_Window = Toplevel(menu)
    New_Window.geometry('500x300')
    calculateButton = tk.Button(New_Window, text = 'calculate', command=calculate)
    calculateButton.place(x=250,y=250)
    for key, item in check_boxes.items():
        if item.get() == 1:
            fooditems2.append(key)
    for x in range(len(fooditems2)):
        b = Label(New_Window, text=fooditems2[x])
        b.pack()
        combobox = ttk.Combobox(New_Window, values=quantity)
        combobox.pack()
    New_Window.mainloop()

def calculate():
    for x in range(len(fooditems2)):
        #cost = fooditems2[x] * combobox.get()
        cost = fooditems[x]
        print(cost)

confirmButton = tk.Button(menu, text = 'Confirm', command=Open)
confirmButton.place(x=250,y=250)



menu.mainloop()


Solution

  • The error is due to that fooditems is a dictionary. To get thing done, you need to find a way that calculate() can reference the price of the selected items and the quantity of the items (the combobox). My suggestion is put these information into fooditems2 list:

    def Open():
        New_Window = Toplevel(menu)
        New_Window.geometry('500x300')
        calculateButton = tk.Button(New_Window, text = 'calculate', command=calculate)
        calculateButton.place(x=250,y=250)
        fooditems2.clear()
        for key, item in check_boxes.items():
            if item.get() == 1:
                Label(New_Window, text=key).pack()
                combobox = ttk.Combobox(New_Window, values=quantity)
                combobox.pack()
                # save the price and the combobox
                fooditems2.append([fooditems[key][0], combobox])
        # make window modal
        New_Window.grab_set()
        New_Window.wait_window(New_Window)
    
    def calculate():
        total = 0
        for price, cb in fooditems2:
            cost = price * int(cb.get())
            print(cost)
            total += cost
        print('total:', total)