我希望你能帮助我使用以下代码并告诉我哪里出错了。我不是那种经验丰富的python,所以帮助会很棒!继承我的代码:
from Tkinter import *
import math
from math import *
import tkFont
import tkMessageBox
numberOfIngredients = {}
diameter = {}
class Application(Frame):
""" GUI application that creates a story based on user input. """
def __init__(self, master):
""" Initialize Frame. """
#Make size later master.geometry("485x280+5+10")
Frame.__init__(self, master)
self.grid()
self.create_widgets()
self.config(bg = "#a6b6cb")
def create_widgets(self):
""" Define the widgets and positioning within the frame. """
#Display the Title
Label(self, text = "Hamburger Calculator", font = ("Calibri", "25"), bg = "#a6b6cb", fg = "#17375e").grid(row = 0, column = 1, sticky = W)
#Instructions For Selecting Box
Label(self, text ="Select your extra ingredients:",
bg = "#a6b6cb").grid(row = 1, column =1, sticky = W)
#Button To Calculate
Button(self, text = "Calculate Cost", bg = "#92d050",
command = self.calculate,
font = ("Calibri", "12")).grid(row = 6, column = 1, sticky = SE)
#Button To Quit
Button(self, text = "Quit", bg = "#913c3a",
command = self.quit,
font = ("Calibri", "12")).grid(row = 7, column = 1,
sticky = SE)
#Radio Box For Patty Size
Label(self, text ="Hamburger Size:",
bg = "#a6b6cb").grid(row = 1, column = 0, sticky = W)
diameter = IntVar()
Radiobutton(self,
text = "10 cm",
bg = "#a6b6cb",
variable = diameter,
value = 10,
).grid(row = 3, column = 0, sticky =W)
Radiobutton(self,
text = "20 cm",
bg = "#a6b6cb",
variable = diameter,
value = 20,
).grid(row = 4, column = 0, sticky =W)
Radiobutton(self,
text = "35 cm",
bg = "#a6b6cb",
variable = diameter,
value = 35,
).grid(row = 5, column = 0, sticky =W)
#Check Box for Cheese
self.cheese = BooleanVar()
Checkbutton(self,
text="Cheese",
bg = "#a6b6cb",
variable = self.cheese,
onvalue="yes", offvalue="no"
).grid(row =3,
column = 1, sticky =W)
#Check Box for Mushrooms
self.mushroom = BooleanVar()
Checkbutton(self,
text="Mushrooms",
bg = "#a6b6cb",
variable = self.mushroom,
onvalue="yes", offvalue="no"
).grid(row =4,
column = 1, sticky =W)
#Check Box for Onions
self.onion = BooleanVar()
Checkbutton(self,
text="Onions",
bg = "#a6b6cb",
variable = self.onion,
onvalue="yes", offvalue="no"
).grid(row =5,
column = 1, sticky =W)
#Check Box for Extra Beefburger
self.beefburger = BooleanVar()
Checkbutton(self,
text="Extra Burger Patty",
bg = "#a6b6cb",
variable = self.beefburger,
onvalue="yes", offvalue="no"
).grid(row =6,
column = 1, sticky =W)
numberOfIngredients = 0
if self.cheese.get() == "yes":
numberOfIngredients + 1
if self.mushroom.get() == "yes":
numberOfIngredients + 1
if self.onion.get() == "yes":
numberOfIngredients + 1
if self.beefburger.get() == "yes":
numberOfIngredients + 1
def calculate(self):
#Constants and Variables
fixedCost = 3.75
baseCost = 1.55
extraCost = 1.25
global numberOfIngredients
global diameter
#Formulas
area = (pi * sqrt(int(diameter) / 100) ) / 4
cost = fixedCost + (baseCost * area) + (numberOfIngredients * extraCost)
totalCost = 1.5 * cost
#Message Box
tkMessageBox.showinfo(
title="Total Cost",
message="The total cost of the hamburger is $" + str(diameter))
#Main
root = Tk()
root.title("Hamburger Calculator")
app = Application(root)
root.mainloop()
root.destroy()
这是我现在得到的错误:
Tkinter回调中的异常 Traceback(最近一次调用最后一次): 文件“C:\ Python27 \ lib \ lib-tk \ Tkinter.py”,第1410行,调用 return self.func(* args) 文件“C:\ Users \ Matt Sharp \ Desktop \ Assessment for Hamburgers v2tester.py”,第131行,在计算中 area =(pi * sqrt(int(diameter)/ 100))/ 4 TypeError:int()参数必须是字符串或数字,而不是'dict'
除了这个错误,我知道我的直径和numberOfIngredients不起作用。
答案 0 :(得分:2)
这是一个非常好的例子,说明为什么你应该避免全局存储你的状态...
在模块的顶部,您可以创建diameter
字典:
diameter = {}
然后在您的create_widgets()
方法中,创建一个要在窗口小部件中使用的局部变量diameter
(尽管您从不保留对它的引用):
diameter = IntVar()
但是在你的calculate()
方法中,你引用全局diameter
(这是一个dict)并在数学表达式中使用它,就好像它是一个int:
area = (pi * sqrt(int(diameter) / 100) ) / 4
你也对numberOfIngredients
做了类似的事情。它被创建为全局字典,但用作本地int而不保存,然后在数学表达式中再次称为全局。
# global numberOfIngredients is {}
cost = fixedCost + (baseCost * area) + (numberOfIngredients * extraCost)
全局变量很容易使您的应用程序混乱,因为您无法始终根据当前范围识别变量的来源。
我不确定您希望如何使用全局字典与本地int,但最终变量应该作为实例属性在类上维护:
class Application(Frame):
def __init__(self, master):
...
self.diameter = IntVar()
self.numberOfIngredients = 0
def create_widgets(self):
...
Radiobutton(self,
text = "10 cm",
bg = "#a6b6cb",
variable = self.diameter,
value = 10,
).grid(row = 3, column = 0, sticky =W)
...
if self.cheese.get() == "yes":
self.numberOfIngredients += 1
def calculate(self):
...
area = (pi * sqrt(int(self.diameter) / 100) ) / 4
cost = fixedCost + (baseCost * area) + \
(self.numberOfIngredients * extraCost)