Reputation: 1
i was hoping you would help me with the following code and tell me where im going wrong. I am not that experienced in python so help would be great! Heres my code:
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()
Here is the error i get at the moment:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in call return self.func(*args) File "C:\Users\Matt Sharp\Desktop\Assessment For Hamburgers v2tester.py", line 131, in calculate area = (pi * sqrt(int(diameter) / 100) ) / 4 TypeError: int() argument must be a string or a number, not 'dict'
As well as this error, i know that my diameter and numberOfIngredients isn't working.
Upvotes: 0
Views: 496
Reputation: 92569
This is a really good example of why you should avoid globals for storing your state...
At the top of your module you create diameter
as a dictionary:
diameter = {}
Then in your create_widgets()
method, you create a local variable diameter
to be used in your widgets (though you never keep a reference to it):
diameter = IntVar()
But then in your calculate()
method, you refer to the global diameter
(which is a dict) and use it in a math expression as if it were an int:
area = (pi * sqrt(int(diameter) / 100) ) / 4
You also do a similar thing with numberOfIngredients
. It is created as a global dict, but used as a local int and not saved, and then referred to as a global again in a math expression.
# global numberOfIngredients is {}
cost = fixedCost + (baseCost * area) + (numberOfIngredients * extraCost)
globals can easily make your application confusing because you cannot always identify the origin of the variable based on current scope.
I am not sure how you expect to use the global dict vs the local int, but ultimately the variables should be maintained on the class as instance attributes:
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)
Upvotes: 2