[Tutor] Tkinter GUI

myles broomes mylesbroomes at hotmail.co.uk
Tue Sep 11 18:51:21 CEST 2012


I'm coding a GUI program which presents the user with a resteraunt menu, and allows them to choose how much of each item they want to purchase before ordering. Here is my code: #Order up GUI program
#User is presented with a simple resturaunt menu
#They then select the items they want to order and then they are presented with the total billfrom tkinter import *class Application(Frame):
    """Based on the frame class, the application widget holds all other widgets. """
    def __init__(self,master):
        """Initialise the frame. """
        super(Application,self).__init__(master)
        self.grid()
        self.create_widgets()
        self.total=0
        
        self.spag_ent_check=True
        self.tater_ent_check=True
        self.soup_ent_check=True
        self.fish_ent_check=True        self.spag_cost=5.15
        self.tater_cost=4.70
        self.soup_cost=4.20
        self.fish_cost=5.95    def create_widgets(self):
        """Create all of the widgets in the program. """
        Label(self,text="Select the items you want to order:").grid(row=0,column=0,sticky=W)
        Label(self,text="Quantity:").grid(row=0,column=0,sticky=E)        Label(self,text="Spaghetti Bolognese (5.15)").grid(row=1,column=0,sticky=W)
        self.spag_ent=Entry(self)
        self.spag_ent.grid(row=1,column=0,sticky=E)        Label(self,text="Potato Salad (4.70)").grid(row=2,column=0,sticky=W)
        self.tater_ent=Entry(self)
        self.tater_ent.grid(row=2,column=0,sticky=E)        Label(self,text="Chicken Soup (4.20)").grid(row=3,column=0,sticky=W)
        self.soup_ent=Entry(self)
        self.soup_ent.grid(row=3,column=0,sticky=E)        Label(self,text="Smoked Salmon (5.95)").grid(row=4,column=0,sticky=W)
        self.fish_ent=Entry(self)
        self.fish_ent.grid(row=4,column=0,sticky=E)        Label(self,text="Total Bill:").grid(row=7,column=0,sticky=W)
        self.bill_txt=Text(self,height=10,wrap=WORD)
        self.bill_txt.grid(row=8,column=0,sticky=W)        Button(self,text="Order Up!",command=self.calc_total).grid(row=5,column=0,sticky=W)    def calc_total(self):
        """Update the text widget if they order Spaghetti Bolognese. """
        try:
            int(spag_ent.get())
        except:
            message="You have to enter a number in the quantity box. \n"
            self.bill_txt.delete(0.0,END)
            self.bill_txt.insert(0.0,message)
            self.spag_ent_check=False # set to False if user enters anything other than an integer in the entry widget        if self.spag_ent_check:
            spag_quant=self.spag_ent.get()
            spag_total=self.spag_cost*spag_quant
            self.total+=spag_total        try:
            int(tater_ent.get())
        except:
            message="You have to enter a number in the quantity box. \n"
            self.bill_txt.delete(0.0,END)
            self.bill_txt.insert(0.0,message)
            self.tater_ent_check=False # set to False if user enters anything other than an integer in the entry widget        if self.tater_ent_check:
            tater_quant=self.tater_ent.get()
            tater_total=self.tater_cost*tater_quant
            self.total+=tater_total        try:
            int(soup_ent.get())
        except:
            message="You have to enter a number in the quantity box. \n"
            self.bill_txt.delete(0.0,END)
            self.bill_txt.insert(0.0,message)
            self.soup_ent_check=False # set to False if user enters anything other than an integer in the entry widget        if self.soup_ent_check:
            soup_quant=self.soup_ent.get()
            soup_total=self.soup_cost*soup_quant
            self.total+=soup_total        try:
            int(fish_ent.get())
        except:
            message="You have to enter a number in the quantity box. \n"
            self.bill_txt.delete(0.0,END)
            self.bill_txt.insert(0.0,message)
            self.fish_ent_check=False # set to False if user enters anything other than an integer in the entry widget        if self.fish_ent_check:
            fish_quant=self.fish_ent.get()
            fish_total=self.fish_cost*fish_quant
            self.total+=fish_total
        
        self.bill_txt.delete(0.0,END)
        self.bill_txt.insert(0.0,self.total)#main
root=Tk()
root.title("Order Up!")
app=Application(root)
root.mainloop()        
The 'calc_total' function is supposed to calculate the users bill and then display it in the text widget, but every time I try it, it comes up as 0. Can anyone help me?  Thanks in advance,Myles Broomes 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120911/a505d73b/attachment-0001.html>


More information about the Tutor mailing list