'How to use loop for my multiplication/division calculator?

This is what I have so far. Can't figure out how to use a loop so that I know where the user clicks and how to use the proper function.

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

This is a basic calculator where the user inputs 2 numbers and either clicks multiply, divide, or exit. All the above is a working code, except bottom loop. I can't figure out what the loop needs in order to figure out where the user clicked.



Solution 1:[1]

Assuming that you are using the graphics.py module, then you need to modify the last few lines:

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

Note that while graphics is intended to be a beginner's package, doing a calculator this way is quite laborious. You should use a more modern method based on an event loop, such as the Tkinter module, which comes with the standard library. Tkinter is more advanced though, so you may want to build up your skills gradually before doing a graphic calculator. I suggest to start with a text based one (i.e. evaluate simple expressions).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel