0

So I'm pretty new to coding and I'm trying to make a simple menu program with tkinter that would let the user click on certain food items and it will display his/her total.

When I run the program python says AttributeError: 'Menu' object has no attribute 'potato_skins'. When I take out "potato skins" it says I do not have the attribute of bread, and so on and so forth.

Can someone please help me, here is the 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()
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Sean Brady
  • 99
  • 6

1 Answers1

3

The reason is that you execute your function instead of passing it as the command parameter.

command = self.update_total()

This means its executed when creating the CheckButton associated with self.motzerella_sticks. At this point self.potato_skins does not exist.

To fix it, pass the function, rather than executing it.

command = self.update_total
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61