2017-08-15 1 views
0

Also bin ich ziemlich neu zu codieren und ich versuche, ein einfaches Menü-Programm mit tkinter, die den Benutzer auf bestimmte Nahrungsmittel klicken würde, und es wird seine anzeigen lassen/sie insgesamt.Python sagt, ich habe kein Attribut, wenn ich das tue

Wenn ich das Programm ausführen Python sagt AttributeError: 'Menu' object has no attribute 'potato_skins'. Wenn ich "Kartoffelschalen" herausnehme, sagt es, dass ich nicht die Eigenschaft von Brot habe und so weiter und so fort.

Kann mir jemand bitte helfen, hier ist der Code:

#Order Up! 
#restaurant menu that lets the user pick foods, then show overall price 

from tkinter import * 

class Menu(Frame): 
    """Menu that let's the user choose food and shows the total price.""" 

    def __init__(self, master): 
     """Initialize the frame""" 
     super(Menu, self).__init__(master) 
     self.grid() 
     self.menu_widgets() 

    def menu_widgets(self): 
     """Create all the menu items.""" 

     #Appetizer Label 
     Label(self, 
       text = "Choose your appetizers:" 
      ).grid(row = 0, column = 0, sticky = W) 
     #Appetizer checkbuttons 
     self.motzerella_sticks = BooleanVar() 
     Checkbutton(self, 
        text = "Mozzerella sticks, $5", 
        variable = self.motzerella_sticks, 
        command = self.update_total() 
        ).grid(row = 1, column = 1, sticky = W) 
     self.potato_skins = BooleanVar() 
     Checkbutton(self, 
        text = "potato skins, $7", 
        variable = self.potato_skins, 
        command = self.update_total() 
        ).grid(row = 1, column = 1, sticky = W) 
     self.bread = BooleanVar() 
     Checkbutton(self, 
        text = "bread, $0", 
        variable = self.bread, 
        command = self.update_total() 
        ).grid(row = 1, column = 2, sticky = W) 

     #Entree Label 
     Label(self, 
       text = "Pick your entree:" 
      ).grid(row = 2, column = 0, sticky = W) 
     #Entree Checkbuttons 
     self.chicken = BooleanVar() 
     Checkbutton(self, 
        text = "chicken and brocolli, $10", 
        variable = self.chicken, 
        command = self.update_total() 
        ).grid(row = 3, column = 0, sticky = W) 
     self.soup = BooleanVar() 
     Checkbutton(self, 
        text = "brocolli cheddar soup, $12", 
        variable = self.soup, 
        command = self.update_total() 
        ).grid(row = 3, column = 1, sticky = W) 
     self.pasta = BooleanVar() 
     Checkbutton(self, 
        text = "alfredo pasta, $15", 
        variable = self.pasta, 
        command = self.update_total() 
        ).grid(row = 3, column = 2, sticky = W) 

     #Dessert Label 
     Label(self, 
       text = "Choose your dessert:" 
      ).grid(row = 4, column = 0, sticky = W) 
     #Dessert Checkbuttons 
     self.cake = BooleanVar() 
     Checkbutton(self, 
        text = "Chocolate cake, $15", 
        variable = self.cake, 
        command = self.update_total() 
        ).grid(row = 5, column = 0, sticky = W) 
     self.brownie = BooleanVar() 
     Checkbutton(self, 
        text = "Brownies, $13", 
        variable = self.brownie, 
        command = self.update_total() 
        ).grid(row = 5, column = 1, sticky = W) 

     #create a Text box to display total 
     self.total_txt = Text(self, width = 40, height = 5, wrap = WORD) 
     self.total_txt.grid(row = 6, column = 0, columnspan = 2, sticky = W) 

    def update_total(self): 
     """Show the total""" 
     total = 0 

     if self.motzerella_sticks.get(): 
      total += 5 

     if self.potato_skins.get(): 
      total += 7 

     if self.bread.get(): 
      total += 0 

     if self.chicken.get(): 
      total += 10 

     if self.soup.get(): 
      total += 12 

     if self.pasta.get(): 
      total += 15 

     if self.cake.get(): 
      total += 15 

     if self.brownie.get(): 
      total += 13 

     self.total_txt.delete(0.0, END) 
     self.total_txt.insert(0.0, "Your total is: ", total) 

#main 
root = Tk() 
root.title("Menu") 
app = Menu(root) 
root.mainloop() 
+1

Es wäre hilfreich, die vollständige Rückverfolgung zu liefern, die die Linie zeigt, in der Dinge brechen –

Antwort

3

Der Grund dafür ist, dass Sie Ihre Funktion statt übergeben Sie als command Parameter ausführen.

command = self.update_total() 

Dies bedeutet, das ausgeführt, wenn die CheckButton mit self.motzerella_sticks assoziiert zu schaffen. Zu diesem Zeitpunkt existiert self.potato_skins nicht.

Um es zu beheben, übergeben Sie die Funktion, anstatt sie auszuführen.

command = self.update_total 
Verwandte Themen