小费计算器 - 语法错误

0 投票
2 回答
581 浏览
提问于 2025-04-18 17:14

大家好,我对编程还比较陌生,但我正在尝试用图形界面来制作一个小费计算器。这个项目不大,也不算太难,但我遇到了一些错误。奇怪的是,我的Python没有显示错误信息。它只是跳到命令行,显示“SyntaxError”(语法错误),然后又回到脚本里。之前是可以显示错误的,但我不知道现在怎么了……总之,如果你们能帮我解决这个问题,我会非常感激的。

`

# A tip calculator
# A tip calculator using a GUI interface
# Austin Howard Aug - 13 - 2014

from tkinter import *
#Creating buttons.
class Calculator(Frame):
    """ A GUI tip calculator."""
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.creating_buttons()
    def creating_buttons(self):
        """This list includes Entry fields, which the user will use to define
several objects such as Bill, and how many people are paying on that bill."""
        #Create an entry field button for how much the bill total is.
        #Create a label 
        bill_lbl = Label(self,
                         text = "Bill: ")
        bill_lbl.grid(row = 1,
                      column = 0,
                      columnspan = 1,
                      sticky = W)
        #Create an Entry field.
        bill_ent = Entry(self)
        bill_ent.grid(row = 1,
                      column = 1,
                      sticky = W)
        #Create a Button for how many people will be paying on the bill
        #Create label
        people_paying_lbl = Label(self,
                                  text = "How many people are paying on this bill?: ")
        people_paying_lbl.grid(row = 2,
                               column = 0,
                               columnspan = 1,
                               sticky = W)
        #Create an entry field
        people_paying_ent = Entry(self)
        people_paying_ent.grid(row = 2,
                               column = 1,
                               sticky = W)
        #Create a text box to display the totals in
        bill_total_txt = Text(self,
                              width = 40,
                              height = 40,
                              wrap = WORD)
        bill_total_txt.grid(row = 3,
                            column = 0,
                            columnspan = 2,
                            sticky = W)
        #Create a Submit button
        submit = Button(self,
                        text = "Submit",
                        command = self.total)
        submit.grid(row = 4,
                    column = 0,
                    sticky = W)

    def total(self):
        """ Takes the values from Bill, and # of people to get the amount that will be
displayed in the text box."""
        TAX = .15
        bill = float(bill_ent)
        people = people_paying_ent
        Total = ("The tip is: $", TAX * bill, "\nThe total for the bill is: $",
                 TAX * bill + bill,
                 "divided among the people paying equally is: $",
                 TAX * bill + bill / people "per, person.")
        bill_total_txt.delete(0.0, END)
        bill_total_txt.insert(0.0, Total)





#Starting the Program
root = Tk()
root.title("Tip Calculator")
app = Calculator(root)
root.mainloop()
`

2 个回答

0

你获取输入框内容的方式不对。你应该把一个字符串变量绑定到这些输入框上,这样才能在后面获取用户输入的内容:

self.billvar = StringVar()
bill_ent = Entry(self, textvariable = self.billvar)

人数输入框也是同样的道理。然后在你的 total 函数里,你可以通过 self.billvar.get() 来读取这些值。你可以用 float(self.billvar.get()) 把这个值转换成浮点数。不过,如果转换失败了(比如用户输入了不能转换成浮点数的内容),你可能想要告诉他们,而不是让程序报错。所以你应该使用类似这样的代码:

try:
    convert input
except:
    what to do if it fails, tell the user?
else:
    what to do if it did not fail, so do the calculations

这样你的程序就变成了这样(我加了注释,标记为 ##):

# A tip calculator
# A tip calculator using a GUI interface
# Austin Howard Aug - 13 - 2014

from tkinter import *
#Creating buttons.
class Calculator(Frame):
    """ A GUI tip calculator."""
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.creating_buttons()

    def creating_buttons(self):
        """This list includes Entry fields, which the user will use to define
        several objects such as Bill, and how many people are paying on that bill."""
        #Create an entry field button for how much the bill total is.
        #Create a label 
        bill_lbl = Label(self,
                         text = "Bill: ")
        bill_lbl.grid(row = 1,
                      column = 0,
                      columnspan = 1,
                      sticky = W)
        #Create an Entry field.
        ## Create a StringVar and link it to the entry box
        self.billvar = StringVar()
        bill_ent = Entry(self, textvariable = self.billvar)
        bill_ent.grid(row = 1,
                      column = 1,
                      sticky = W)
        #Create a Button for how many people will be paying on the bill
        #Create label
        people_paying_lbl = Label(self,
                                  text = "How many people are paying on this bill?: ")
        people_paying_lbl.grid(row = 2,
                               column = 0,
                               columnspan = 1,
                               sticky = W)
        #Create an entry field
        ## Create a StringVar and link it to the entry box
        self.pplvar = StringVar()
        people_paying_ent = Entry(self, textvariable = self.pplvar)
        people_paying_ent.grid(row = 2,
                               column = 1,
                               sticky = W)
        #Create a text box to display the totals in
        ## This had to be self.bill_total_txt, to be able to put text in it from the total function
        self.bill_total_txt = Text(self,
                              height = 10,
                              width = 40,
                              wrap = WORD)
        self.bill_total_txt.grid(row = 3,
                            column = 0,
                            columnspan = 2,
                            sticky = W)
        #Create a Submit button
        submit = Button(self,
                        text = "Submit",
                        command = self.total)
        submit.grid(row = 4,
                    column = 0,
                    sticky = W)

    def total(self):
        """ Takes the values from Bill, and # of people to get the amount that will be
        displayed in the text box."""
        TAX = .15
        ## Try to convert the bill to a float and the number of people to an integer
        try:
            bill = float(self.billvar.get())
            people = int(self.pplvar.get())
        ## If something goes wrong tell the user the input is invalid
        except:
            self.bill_total_txt.delete(0.0, END)
            self.bill_total_txt.insert(0.0, 'Invalid input')
        ## If the conversion was possible, calculate the tip, the total amount and the amout per person and format them as a string with two decimals
        ## Then create the complete message as a list and join it togeather when writing it to the textbox
        else:
            tip = "%.2f" % (TAX * bill)
            tot = "%.2f" % (TAX * bill + bill)
            tot_pp = "%.2f" % ((TAX * bill + bill) / people)
            Total = ["The tip is: $", tip,
                     "\nThe total for the bill is: $",
                     tot,
                     " divided among the people paying equally is: $",
                     tot_pp,
                     " per person."]
            self.bill_total_txt.delete(0.0, END)
            self.bill_total_txt.insert(0.0, ''.join(Total))





#Starting the Program
root = Tk()
root.title("Tip Calculator")
app = Calculator(root)
root.mainloop()
1

你在第68行有个错误:

TAX * bill + bill / people "per, person.")

替换成

TAX * bill + bill / people, "per, person.")

另外,确保在root.mainloop()后面去掉反引号。

撰写回答