这就是我目前所拥有的。我不知道如何使用循环,以便知道用户单击的位置以及如何使用正确的函数。你知道吗
import math
from graphics import *
def main():
#Create window to hold other objects
win = GraphWin("Calculator", 700, 400)
#Set color to blue
win.setBackground("azure")
#Set coordinate system
win.setCoords(0, 0, 4, 4)
#Display instructions for user
prompt = Text(Point(2, 3.75),
"Enter two numbers and click an operand, or exit.")
prompt.draw(win)
#Setup calc boxes
o1 = Text(Point(0.65, 3), "Operand 1")
o1.draw(win)
o2 = Text(Point(2.65, 3), "Operand 2")
o2.draw(win)
tres = Text(Point(3.5, 3), "Result")
tres.draw(win)
#Draw entry box
e1 = Entry(Point(1, 3), 5)
e1.setText("0")
e1.setSize(10)
e1.setFill(color_rgb(200, 255, 255))
e1.draw(win)
e2 = Entry(Point(3, 3), 5)
e2.setText("0")
e2.setSize(10)
e2.setFill(color_rgb(200, 255, 255))
e2.draw(win)
resultrec = Entry(Point(3.8, 3), 5)
resultrec.setText("")
resultrec.setSize(10)
resultrec.setFill(color_rgb(200, 255, 255))
resultrec.draw(win)
#Operations
multrec = Rectangle(Point(.5, 2.3), Point(1.5, 1.75))
multrec.draw(win)
tmult = Text(Point(1, 2), "Multiply")
tmult.draw(win)
divrec = Rectangle(Point(2.5, 2.3), Point(3.5, 1.75))
divrec.draw(win)
tdiv = Text(Point(3, 2), "Divide")
tdiv.draw(win)
exitrec = Rectangle(Point(1.75, .75), Point(2.3, .2))
exitrec.draw(win)
texit = Text(Point(2, .5), "Exit")
texit.draw(win)
exit = False
while(not exit):
win.getMouse()
multrect == calc_mulity
main()
这是一个基本的计算器,用户输入2个数字,然后单击乘、除或退出。上面所有的代码都是工作代码,除了底部循环。我无法确定循环需要什么才能确定用户单击的位置。你知道吗
假设您正在使用graphics.py模块,那么您需要修改最后几行:
请注意,虽然
graphics
是一个初学者的软件包,但用这种方法编写计算器相当费劲。您应该使用基于事件循环的更现代的方法,例如标准库附带的Tkinter
模块。不过,Tkinter更先进,所以在使用图形计算器之前,你可能需要逐步积累技能。我建议从一个基于文本的开始(即评估简单表达式)。你知道吗相关问题 更多 >
编程相关推荐