1

So, I have receipt section on my project which will print out all the list of food on my page. But I just want this to print only the quantity of the checked box. Here's my code.

def Receipt():

        txtReceipt.insert(END,' \t\t *FAST FOOD CAFE* \n\n')
        txtReceipt.insert(END,'Date: '+ DateofOrder.get() +'\n')
        txtReceipt.insert(END,'Items\t\t\t\t'+"Cost of Items \n")
        txtReceipt.insert(END,'Icecream:\t\t\t\t\t' + E_Icecream.get() +'\n')
        txtReceipt.insert(END,'Donut:\t\t\t\t\t'+ E_Donut.get()+'\n')
        txtReceipt.insert(END,'Softdrink:\t\t\t\t\t'+ E_Softdrink.get()+'\n')
        txtReceipt.insert(END,'HotDog:\t\t\t\t\t'+ E_HotDog.get()+'\n')
        txtReceipt.insert(END,'Pizza:\t\t\t\t\t'+ E_Pizza.get()+'\n')
        txtReceipt.insert(END,'Taco:\t\t\t\t\t'+ E_Taco.get()+'\n')
        txtReceipt.insert(END,'Hamburger:\t\t\t\t\t'+ E_Hamburger.get()+'\n')
        txtReceipt.insert(END,'Fries:\t\t\t\t\t'+ E_Fries.get()+'\n')
        txtReceipt.insert(END,'Wings:\t\t\t\t\t'+ E_Wings.get()+'\n')
        txtReceipt.insert(END,'Cost of Drinks:\t\t\t\t'+ CostofDrinks.get()+'\nCost of Foods:\t\t\t\t'+ CostofFood.get()+"\n")
        txtReceipt.insert(END,'Delivery Charge:\t\t\t\t'+ ServiceCharge.get()+'\nTotal Cost:\t\t\t\t'+str(TotalCost.get())+"\n")
        txtReceipt.insert(END,'\nSpecial Request:   '+ Request.get())
rgsummer
  • 11
  • 2
  • Hi rgsummer, welcome to StackOverflow! I'd recommend reading through https://stackoverflow.com/help/how-to-ask and updating your question to make the advice from there. As part of that, it's always good to include a MVE (https://stackoverflow.com/help/minimal-reproducible-example) to help people understand your code. Right now, it's a little unclear what your code is actually doing, because it seems to reference objects that are outside of the given method. – Brydenr Nov 26 '19 at 16:20
  • I'm so sorry, Sir. I'm just new here. Btw, Thank you! Will surely do this if ever I ask a question again. – rgsummer Nov 27 '19 at 07:56

1 Answers1

0

Arguably, the best solution is to put your checkbuttons and entry widgets or variables in a dictionary or list, and then iterate over the values.

For example, you could create the checkbuttons like this:

items = ("Icecream", "Donut", "Softdrink", "HotDog")
vars = {}
entries = {}
for row, item in enumerate(items):
    var = tk.BooleanVar(value=False)
    cb = tk.Checkbutton(root, text=f"{item}:", variable=var, onvalue=True, offvalue=False)
    entry = tk.Entry(root)
    vars[item] = var
    entries[item] = entry

    cb.grid(row=row, column=0, sticky="w")
    entry.grid(row=row, column=1, sticky="ew")

With your vars and entries dictionaries initialized, to print out the receipt you can simply iterate over them to get the checked items:

def receipt():
    for item in items:
        selected = vars[item].get()
        if selected:
            txtReceipt.insert("end", f"{item}: {entries[item].get()}\n")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685