0

我希望你能用下面的代码帮助我,并告诉我我哪里出错了。我在 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 不起作用。

4

1 回答 1

2

这是一个很好的例子,说明了为什么你应该避免使用全局变量来存储你的状态......

在您的模块顶部,您创建diameter一个字典:

diameter = {}

然后在您的create_widgets()方法中,您创建一个diameter要在小部件中使用的局部变量(尽管您从不保留对它的引用):

diameter = IntVar() 

但是然后在您的calculate()方法中,您引用全局diameter(这是一个字典)并在数学表达式中使用它,就好像它是一个 int 一样:

area = (pi * sqrt(int(diameter) / 100) ) / 4

你也对numberOfIngredients. 它被创建为全局 dict,但用作本地 int 且未保存,然后在数学表达式中再次称为全局。

# global numberOfIngredients is {}
cost = fixedCost + (baseCost * area) + (numberOfIngredients * extraCost)

全局变量很容易使您的应用程序混乱,因为您不能总是根据当前范围识别变量的来源。

我不确定您希望如何使用全局 dict 与本地 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)
于 2012-08-01T20:59:41.923 回答