如何在方法中调用金额

时间:2015-04-21 13:55:30

标签: python methods tkinter

# Tip Calculator
from tkinter import *

class TipCalc:
    def __init__(self):
        my_window = Tk() # create a window
        my_window.title("Tip Calculator")
        my_window.geometry('400x200')

        amount = StringVar()

        # create frames labels, entry, and buttons
        frm_upper = Frame(my_window)
        frm_upper.pack()
        frm_mid = Frame(my_window)
        frm_mid.pack()
        frm_lower = Frame(my_window)
        frm_lower.pack()

        # create label and entry for amount
        amount_label = Label(frm_upper, text = "Enter Amount: ")
        my_entry = Entry(frm_upper, textvariable = amount)
        my_entry.delete(0, END)
        my_entry.insert(0, "")

        # create a label for total amount
        total_label = Label(frm_mid, text = "")

        # create buttons in lower frame
        btn_10_perc = Button(frm_lower, text = "10%", command = self.calculate_10_perc)
        btn_15_perc = Button(frm_lower, text = "15%", command = self.calculate_15_perc)
        btn_18_perc = Button(frm_lower, text = "18%", command = self.calculate_18_perc)
        btn_20_perc = Button(frm_lower, text = "20%", command = self.calculate_20_perc)

        amount_label. grid(row = 1, column = 1)
        my_entry.    grid(row = 1, column = 2)
        btn_10_perc. grid(row = 3, column = 1)
        btn_15_perc. grid(row = 3, column = 2)
        btn_18_perc. grid(row = 3, column = 3)
        btn_20_perc. grid(row = 3, column = 4)

        my_window.mainloop()

    def calculate_10_perc(self):
        bill = float(self.amount.get())
        total = bill*1.10
        total_result = Label(frm_mid, "Total after tip added: %.2f" % total).grid(row=2, column =1)
        return

    def calculate_15_perc(self):
        pass

    def calculate_18_perc(self):
        pass

    def calculate_20_perc(self):
        pass


########################### Main Program ###############################
TipCalc() # anonymous instance of the class

如何将实例中的金额调入方法,以便我可以用它来计算?我收到了一个错误,

  

AttributeError:'TipCalc'对象没有属性'amount'

2 个答案:

答案 0 :(得分:6)

您必须将amount绑定到self,即

amount = StringVar()

应该是

self.amount = StringVar()

因此,您还必须将my_entry = Entry(frm_upper, textvariable=amount)更改为my_entry = Entry(frm_upper, textvariable=self.amount)

否则,self.amount中执行bill = float(self.amount.get())时未定义calculate_10_perc


另外,请注意做

total_result = Label(...).grid(row=2, column=1)

total_result 不是 Label,而是grid的结果,即None

每次更新total值时,您似乎都在(尝试)创建新标签。相反,您应该configure现有标签来保存新值。在__init__

self.total_label = Label(frm_mid, text="")
self.total_label.pack()

calculate_10_perc

self.total_label.configure(text="Total after tip added: %.2f" % total)

最后,鉴于所有这些calculate_X_perc函数可能看起来非常相似,您可以将其作为一个函数并将百分比作为参数传递,例如:在__init__

btn_10_perc = Button(frm_lower, text = "10%", command=lambda: self.calculate_perc(1.1))

功能:

def calculate_perc(self, percent):
    bill = float(self.amount.get())
    self.total_label.configure(text="Total after tip added: %.2f" % (bill * percent))

答案 1 :(得分:4)

好的,这里的问题是你没有在amount中将__init__作为实例变量:

class TipCalc:
    def __init__(self):
        my_window = Tk() # create a window
        my_window.title("Tip Calculator")
        my_window.geometry('400x200')

        amount = StringVar()
        ...

请注意,您只是声明amount,而不是self.amount。这意味着amount__init__中的正常变量,在__init__完成时消失,而不是只要实例存在就存在的实例变量。

然后你在这里收到错误:

def calculate_10_perc(self):
    bill = float(self.amount.get())

由于self.amount此时不是amount__init____init__完成时消失了。

因此,最简单的解决方案可能就是简单地更改amount并使class TipCalc: def __init__(self): my_window = Tk() # create a window my_window.title("Tip Calculator") my_window.geometry('400x200') self.amount = StringVar() ... 成为实例变量:

amount

然后,请务必将所有对self.amount的引用替换为{{1}}。