Python GUI图形计算器

时间:2016-11-17 21:46:29

标签: python user-interface calculator

我正在尝试从图形库中制作商店计算器,而我的程序由于未知原因而不断崩溃。每当我点击计算按钮时,它就会停止运行而我没有输出。这就是我现在所拥有的:

#   graphical interface.
from graphics import *
def main():
win = GraphWin("Celsius Converter", 600, 500)
win.setCoords(0.0, 0.0, 3.0, 4.0)

# Draw the interface
Text(Point(1,3.9), "   Enter name:").draw(win)
Text(Point(1,3.7), "   Crunchy tacos at $1.99 each:").draw(win)
Text(Point(1,3.5), "   Soft tacos at $2.09 each:").draw(win)
Text(Point(1,3.3), "   Bean burritos at $2.49 each:").draw(win)
Text(Point(1,3.1), "   Chicken burritos at $2.99 each:").draw(win)
Text(Point(1,2.9), "   Taco salads at $3.49 each:").draw(win)
Text(Point(1,2.7), "   Extra salsa at $.30 each:").draw(win)
Text(Point(1,1), "Due:").draw(win)
input1 = Entry(Point(2,3.9), 5)
input1.setText("Name")
input1.draw(win)
input2 = Entry(Point(2,3.7), 5)
input2.setText("0")
input2.draw(win)
input3 = Entry(Point(2,3.5), 5)
input3.setText("0")
input3.draw(win)
input4 = Entry(Point(2,3.3), 5)
input4.setText("0")
input4.draw(win)
input5 = Entry(Point(2,3.1), 5)
input5.setText("0")
input5.draw(win)
input6 = Entry(Point(2,2.9), 5)
input6.setText("0")
input6.draw(win)
input7 = Entry(Point(2,2.7), 5)
input7.setText("0")
input7.draw(win)
output = Text(Point(2,1),"")
output.draw(win)
button = Text(Point(1.5,2.0),"Calculate")
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# wait for a mouse click
win.getMouse()

# convert input
name = input1.getText()
crunchy = float(input2.getText()) * 1.99
soft = float(input3.getText()) * 2.09
bean = float(input4.getText()) * 2.49
chicken = float(input5.getText()) * 2.99
salad = float(input6.getText()) * 3.49
salsa = float(input7.getText()) * 0.30
costBefore = (crunchy + soft + bean + chicken + salad + salsa)
tax = 0.075 * costBefore
cost = costBefore + tax

# display output and change button
output.setText("Hello", name, ",your total is: ", cost)
button.setText("Quit")

# wait for click and then quit
win.getMouse()
win.close()

main()

1 个答案:

答案 0 :(得分:1)

setText()不是print(),它需要一个参数

output.setText( "Hello {}, your total is: {}".format(name, cost) )