Python Forum
Referencing Combo Box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Referencing Combo Box
#1
Hi - 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:
Error:
cost = fooditems2[x] * combobox.get() TypeError: can't multiply sequence by non-int of type 'str'
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()
        print(cost)

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



menu.mainloop()
Reply
#2
cost calculation is commented out on line 40
Reply
#3
(Feb-12-2020, 02:54 AM)Larz60+ Wrote: cost calculation is commented out on line 40

Whoops forgot to uncomment when I was testing. Fixed the error message in OP.
Reply
#4
Quote:Fixed the error message in OP.
Is that the complete unaltered error traceback?
I was expecting more
Reply
#5
(Feb-12-2020, 03:05 AM)Larz60+ Wrote:
Quote:Fixed the error message in OP.
Is that the complete unaltered error traceback?
I was expecting more

This was the entire message

Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\aaaaa\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "C:\Users\aaaaa\Documents\pizzamenu.py", line 40, in calculate cost = fooditems2[x] * combobox.get() TypeError: can't multiply sequence by non-int of type 'str'
Reply
#6
I think this is what it's complaining about (haven't tested):
cost = fooditems2[x] * int(combobox.get())
pretty sure combobox.get is string by default
Reply
#7
A simple print of fooditems2[x] and combobox.get() will tell you what is what. It looks like fooditems2 contains the description and not the cost but you will have to verify that yourself.
    for key, item in check_boxes.items():
        if item.get() == 1:
            fooditems2.append(key)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo PySimpleGUI FilesBrowse enable event to update value in combo SamLiu 2 4,455 Mar-15-2023, 12:49 PM
Last Post: SamLiu
  How to update the list of a combo box in a QTableView panoss 10 6,118 Feb-05-2022, 03:24 PM
Last Post: panoss
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 5,395 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  Need help adding a sql List to a combo box PyQt5 jimmyvegas29 1 8,555 Jul-20-2018, 07:28 AM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020