Home > Back-end >  Displaying values of checkbuttons in python tkinter
Displaying values of checkbuttons in python tkinter

Time:12-14

I have created a list of 5 checkbuttons, displaying 5 different pizzas to choose. When one or more buttons are clicked it will calculate the total price of pizzas. I have a function with for loop and if statement excuting the price of pizza when clicked. How can I make the output display the total price of pizzas if one or more pizzas are clicked but also display "you have ordered no pizza" when none have been clicked? In my if statement it is displaying the total price of pizzas clicked but also displaying "you have ordered no pizza" due to the other choices not being clicked. I need it to display "you ordered no pizza" when only no buttons have been clicked.

def total():
    top = Toplevel()
    tot = 0
    name = entry1.get()
    address = entry2.get()
    for pizza,var in zip(pizzas,var_list):
        if var.get() != 0:
            tot = tot   var.get()
            label = Label(top, text="Your total cost of pizza is ${}\nShipping to {},\n at this address: {}".format(tot, name, address), font='helvetica, 32').grid(row=9, column=0)
        else:
            label1 = Label(top, text='you ordered no pizza').grid(row=11, column=0)
    button = Button(top, text='Exit', command=top.destroy).grid(row=10, column=0)

CodePudding user response:

You need to check whether tot > 0 after the for loop to determine which message to be shown:

def total():
    top = Toplevel()
    tot = 0
    name = entry1.get()
    address = entry2.get()
    for pizza,var in zip(pizzas, var_list):
        price = var.get()
        if price != 0:
            tot  = price

    if tot > 0:
        Label(top, text="Your total cost of pizza is ${}\nShipping to {},\n at this address: {}".format(tot, name, address), font='helvetica, 32').grid(row=9, column=0)
    else:
        Label(top, text='you ordered no pizza').grid(row=11, column=0)

    Button(top, text='Exit', command=top.destroy).grid(row=10, column=0)

Note that assigning Label(...).grid(...) (always None) to a variable is meaningless.

  • Related