简体   繁体   English

提示计算器-语法错误

[英]Tip calculator - syntax error

Hello guys im new to programming relatively, but all the same im trying to use a GUI interface to build a tip calculator. 大家好,我是相对较不熟悉编程的人,但是他们都试图使用GUI界面来构建小费计算器。 nothing big, nothing relatively hard, but im running into errors. 没有什么大的,没有什么相对困难的,但是我遇到了错误。 for some reason my Python wont show the errors. 由于某种原因,我的Python不会显示错误。 it just goes to the shell, says SyntaxError: and then quits back to the script. 语法错误:它只是进入外壳,然后返回脚本。 it used to show the errors but i dont know whats wrong... anyways if you guys could help me troubleshoot this id greatly appreciate it.. 它曾经显示错误,但我不知道是怎么回事...无论如何,如果你们可以帮助我解决此ID问题,我们将不胜感激。

` `

# 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()
`

You have an error on line 68: 您在第68行有一个错误:

Replace 更换

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

with

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

Also make sure you remove the backtick after root.mainloop() 还要确保在root.mainloop()之后删除反引号

Your way of getting the input from the Entry boxes is wrong. 您从输入框获取输入的方法是错误的。 You should bind a StringVariable to them, to later be able to get what the user typed: 您应该将StringVariable绑定到它们,以便以后能够获得用户键入的内容:

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

And the same for the number of people box. 和人数框相同。 Then in your total function you can read the values by using self.billvar.get() . 然后,在total函数中,您可以使用self.billvar.get()读取值。 You can convert this to a float using float(self.billvar.get()) . 您可以使用float(self.billvar.get())将其转换为float。 However, when this fails (the user typed in something that can not be converted to a float) you probably want to tell them instead of having the program throwing an error. 但是,如果失败(用户键入了不能转换为浮点数的内容),则可能要告诉他们,而不是让程序抛出错误。 So you should use something like: 因此,您应该使用类似:

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

Your program then becomes something like this (I made comments with ##): 然后,您的程序将变成这样(我使用##进行了注释):

# 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM