Python:我正在研究餐厅小费计算器,我正在尝试集成单选按钮

时间:2014-06-22 19:24:12

标签: python tkinter radio-button

我需要知道如何将选定的单选按钮实现到我的计算中。感谢您的帮助!我真的不肯定问题是什么,我唯一的任务真的来自“def选择”部分。我只是不知道该怎么做

from Tkinter import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)

        self.headerFont = ("Times", "16", "italic")
         self.title("Restaurant Tipper")
        self.addOrigBill()
        self.addChooseOne()
        self.addPercTip()
        self.addRateTip()
        self.addOutput()

    def addOrigBill(self):
        Label(self, text = "Bill Amount",
            font = self.headerFont).grid(columnspan = 1)
        self.txtBillAmount = Entry(self)
        self.txtBillAmount.grid(row = 1, column = 1)
        self.txtBillAmount.insert(0,"100.00")

    def addChooseOne(self):
        Label(self, text = "Pick ONE! Choose your % of Tip or Rate your experience",
            font = self.headerFont).grid(row = 2, column = 1)

    def addPercTip(self):
        Label(self, text = "% of Tip", 
            font = self.headerFont).grid(row = 3, column = 0)

        self.radPercTip1 = Radiobutton(self, text = "15%", 
            variable = self.percVar, value = .15, command = self.selected)
        self.radPercTip2 = Radiobutton(self, text = "17%", 
            variable = self.percVar, value = .17, command = self.selected)
        self.radPercTip3 = Radiobutton(self, text = "20%", 
            variable = self.percVar, value = .20, command = self.selected)

        self.radPercTip1.grid(row = 4, column = 0)
        self.radPercTip2.grid(row = 5, column = 0)
        self.radPercTip3.grid(row = 6, column = 0)

    def selected(self):
        float(self.percVar.get())

    def addRateTip(self):
        Label(self, text = "Tip by rating").grid(row = 3, column = 3)
        Label(self, text = "1 being the worst").grid(row = 4, column = 3)
        Label(self, text = "10 being the best").grid(row = 5, column = 3)
        Label(self, text = "Experience").grid(row = 6, column = 2)

        self.txtExperience = Entry(self)
        self.txtExperience.grid(row = 6, column = 3)

    def addOutput(self):
        self.btnCalc = Button(self, text = "Calculate Tip")
        self.btnCalc.grid(row = 7, columnspan = 2)
        self.btnCalc["command"] = self.calculate

        Label(self, text = "Tip").grid(row = 8, column = 1)
        self.lblTip = Label(self, bg = "#ffffff", anchor = "w", relief = "ridge")
        self.lblTip.grid(row = 8, column = 2, sticky = "we")

        Label(self, text = "Total Bill").grid(row = 9, column = 1)
        self.lblTotalBill = Label(self, bg = "#ffffff", anchor = "w", relief = "ridge")
        self.lblTotalBill.grid(row = 9, column = 2, sticky = "we")


    def calculate(self):
        bill = float(self.txtBillAmount.get())
        percTip = self.percVar
        rateTip = int(self.addRateTip.get())

        tip = bill * percTip
        self.lblTip["text"] = "%.2f" % tip

        totalBill = tip + bill
        self.lblTotalBill["text"] = "%.2f" % totalBill

        if rateTip <= 2:
            percTip = .10

        elif 3 <= rateTip <= 4:
            percTip = .12

        elif 5 <= rateTip <= 6:
            percTip = .15

        elif 7 <= rateTip <= 8:
            percTip = .17

        elif 9 <= rateTip <= 10:
            percTip = .20

        else:
            self.lblTotalBill["text"] = "Something is wrong"
def main():
    app = App()
    app.mainloop()

if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:0)

您忘了初始化self.percVar变量

添加self.percVar = 0作为addPercTip方法的第一行。这应该可以解决你的错误。

BTW:对于未来的问题,最好的办法是包含你得到的错误信息,并准确描述该程序的哪一部分会给你带来什么样的麻烦

答案 1 :(得分:0)

代码中没有self.percVar。正如@wastl所说,你需要初始化它。

为此,您需要使用variable classes之一。由于您使用的是float类型,因此DoubleVar()将是最佳选择。

def addPercTip(self):
    self.percVar = DoubleVar()  #this line should be added in your method
    Label(self, text = "% of Tip", 
        font = self.headerFont).grid(row = 3, column = 0)

def selected(self):
    print (type(self.percVar.get())) 
    #which is float, without converting explicitly because of DoubleVar()
    print (self.percVar.get())
    #this will print what you click