I have a problem. I want to write a program in which there are dishes and prices, and they are selected using Checkbutton. After pressing “Place order”, the sum of the prices of the selected dishes is displayed.
My “if” condition displays the price of only the first selected dish. The solution is certainly simple, but I’m just learning and I can’t figure out how to write it.
from tkinter import*
class Application(Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
Label(self, text="Select the dish you want to order").grid(row=0,column=0,columnspan=2,sticky=W)
self.position_1=BooleanVar()
Checkbutton(self,text="Hamburger_________25$",variable=self.position_1).grid(row=1,column=0,columnspan=2,sticky=W)
self.position_2=BooleanVar()
Checkbutton(self,text="Fish and chips_________15$",variable=self.position_2).grid(row=2,column=0,columnspan=2,sticky=W)
self.position_3=BooleanVar()
Checkbutton(self,text="Greek salad_________28$",variable=self.position_3).grid(row=3,column=0,columnspan=2,sticky=W)
self.position_4=BooleanVar()
Checkbutton(self,text="Carbonara pasta_________32$",variable=self.position_4).grid(row=4,column=0,columnspan=2,sticky=W)
Button(self,text="Place order",command=self.bill).grid(row=5,column=0,sticky=W)
self.text=Text(self,width=100,heigh=100,wrap=WORD)
self.text.grid(row=6,column=0,columnspan=2,sticky=W)
def bill(self):
saldo=0
if self.position_1.get():
saldo+=25
if self.position_2.get():
saldo+=15
elif self.position_3.get():
saldo+=28
elif self.position_4.get():
saldo+=32
message="The final sum is:"+str(saldo)+"$."
self.text.delete(0.0,END)
self.text.insert(0.0,message)
root=Tk()
root.title("Menu with GUI")
app=Application(root)
root.mainloop()
Thanks!
The program only shows the price of the first selected item.
Aleksandra Mucha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.