我希望你能用下面的代码帮助我,并告诉我我哪里出错了。我在 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 行,调用 返回 self.func(*args) 文件“C:\Users\ Matt Sharp\Desktop\Assessment For Hamburgers v2tester.py",第 131 行,计算面积 = (pi * sqrt(int(diameter) / 100) ) / 4 TypeError: int() argument must be a string or a number, not '听写'
除了这个错误,我知道我的直径和 numberOfIngredients 不起作用。