所以我对编码很陌生,我试图用tkinter
创建一个简单的菜单程序,让用户点击某些食物项目,它会显示他/她的总数。
当我运行程序时,python说AttributeError: 'Menu' object has no attribute 'potato_skins'
。当我拿出"土豆皮"它说我没有面包的属性,依此类推。
有人可以帮助我,这是代码:
#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()
答案 0 :(得分:3)
原因是您执行了函数而不是将其作为command
参数传递。
command = self.update_total()
这意味着在创建与CheckButton
相关联的self.motzerella_sticks
时执行此操作。此时self.potato_skins
不存在。
要修复它,请传递函数,而不是执行它。
command = self.update_total