python中文语法提示_提示计算器语法

这篇博客介绍了一个使用Tkinter库创建的GUI提示计算器。程序包含输入框来获取账单总额和人数,通过尝试将输入转换为浮点数进行计算,如果输入无效则显示错误信息。计算包括小费、总金额和人均金额,并在文本框中展示结果。该程序展示了基础的GUI交互和异常处理技巧。
摘要由CSDN通过智能技术生成

输入框的输入方式错误。您应该将StringVariable绑定到它们,以便以后能够获取用户键入的内容:self.billvar = StringVar()

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

人数也一样。

然后在total函数中,可以使用self.billvar.get()读取值。可以使用float(self.billvar.get())将其转换为浮点。但是,当这失败时(用户键入的内容不能转换为浮点),您可能希望告诉他们,而不是让程序抛出错误。所以你应该使用类似于:

^{pr2}$

你的程序就会变成这样(我用##做了评论):# 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值