Python:我在做一个餐厅小费计算器,尝试集成单选按钮

0 投票
2 回答
609 浏览
提问于 2025-04-18 10:42

我需要知道怎么把选中的单选按钮放进我的计算中。谢谢大家的帮助!我其实不太确定问题出在哪里,我的疑问主要来自于“def selection”这一部分。我就是不知道该怎么处理那里。

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

你忘记初始化 self.percVar 这个变量了。

addPercTip 方法的第一行加上 self.percVar = 0,这样就能解决你的错误了。

顺便说一下,以后提问的时候,最好把你遇到的错误信息也一起发上来,并且准确描述一下程序的哪个部分出现了什么问题。

0

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

要做到这一点,你需要使用一种 变量类。因为你使用的是 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

撰写回答