这是我到目前为止所拥有的。无法弄清楚如何使用循环,以便我知道用户点击的位置以及如何使用正确的函数。
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个数字,然后点击乘,除或退出。以上所有都是一个工作代码,底部循环除外。我无法弄清楚循环需要什么来确定用户点击的位置。
答案 0 :(得分:0)
假设您使用的是graphics.py模块,那么您需要修改最后几行:
while(not exit):
point = win.getMouse()
# here you must handle the user input appropriately,
# detecting what has been pressed and act accordingly
# ...
multrect == calc_mulity
请注意,虽然graphics
旨在成为初学者的软件包,但以这种方式执行计算器非常费力。您应该使用基于事件循环的更现代的方法,例如标准库附带的Tkinter
模块。虽然Tkinter更先进,但您可能希望在进行图形计算器之前逐渐建立自己的技能。我建议从基于文本的文本开始(即评估简单表达式)。