so I have the code where if the user clicks on the rectangle or button that says "Calculate" it'll do everything correctly. However, how do I write a code that will ignore all other clicks until they click inside the rectangle or "Calculate" I tried using while loops but I just can't get it to work. I was wondering if anyone was able to help me with my issue.
Here is my code:
from graphics import *
def main():
win = GraphWin("Body Mass Index Calculator", 500, 400)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# Draw the interface
Text(Point(1,3.5), " Height:").draw(win)
Text(Point(1.66,3.5), " ft").draw(win)
Text(Point(2.56,3.5), " inches").draw(win)
Text(Point(1,2.8), " Weight:").draw(win)
Text(Point(1.69,2.8), " lbs").draw(win)
Text(Point(1.5,1), "BMI:").draw(win)
Text(Point(0.5, 1.5), "BMI Categeories:").draw(win)
under = Text(Point(0.56, 1.3), "Underweight - < 18.5").draw(win)
normal = Text(Point(0.65, 1.1), "Normal - > 18.5 and < 25").draw(win)
over = Text(Point(0.71, 0.9), "Overweight - >= 25 and < 30").draw(win)
obese = Text(Point(0.4, 0.7), "Obese - > 30").draw(win)
#for height, inches and ft
#ft
input1 = Entry(Point(1.5,3.5), 5)
input1.setText("0.0")
input1.draw(win)
#inch
input2 = Entry(Point(2.3,3.5), 5)
input2.setText("0.0")
input2.draw(win)
#for weight in lbs
input3 = Entry(Point(1.5,2.8), 5)
input3.setText("0.0")
input3.draw(win)
#calculate box
output = Text(Point(2.1,1),"")
output.draw(win)
button = Text(Point(1.5,2.0),"Calculate")
button.draw(win)
rPoint1 = Point(1, 1.7)
rPoint2 = Point(2,2.3)
rec = Rectangle(rPoint1, rPoint2)
rec.draw(win)
#wait for a mouse click
p = win.getMouse()
#convert
ft = float(input1.getText()) * 12
inch = float(input2.getText())
height = ft + inch
weight = float(input3.getText())
bmi = (703*weight) / (height**2)
#mouse click inside the calculate button
if (p.getX() > rPoint1.getX()) and (p.getX() < rPoint2.getX()):
if (p.getY() > rPoint1.getY()) and (p.getY() < rPoint2.getY()):
if bmi < 18.5:
under.setFill("red")
output.setText(str(bmi))
button.setText("Quit")
if (p.getX() > rPoint1.getX()) and (p.getX() < rPoint2.getX()):
if (p.getY() > rPoint1.getY()) and (p.getY() < rPoint2.getY()):
# wait for click and then quit
win.getMouse()
win.close()
elif bmi >= 18.5 and bmi < 25:
normal.setFill("green")
output.setText(str(bmi))
button.setText("Quit")
if (p.getX() > rPoint1.getX()) and (p.getX() < rPoint2.getX()):
if (p.getY() > rPoint1.getY()) and (p.getY() < rPoint2.getY()):
# wait for click and then quit
win.getMouse()
win.close()
elif bmi >= 25 and bmi < 30:
over.setFill("orange")
output.setText(str(bmi))
button.setText("Quit")
if (p.getX() > rPoint1.getX()) and (p.getX() < rPoint2.getX()):
if (p.getY() > rPoint1.getY()) and (p.getY() < rPoint2.getY()):
# wait for click and then quit
win.getMouse()
win.close()
elif bmi > 30:
obese.setFill("red")
output.setText(str(bmi))
button.setText("Quit")
if (p.getX() > rPoint1.getX()) and (p.getX() < rPoint2.getX()):
if (p.getY() > rPoint1.getY()) and (p.getY() < rPoint2.getY()):
# wait for click and then quit
win.getMouse()
win.close()
main()
Is this for Professor Kate's assignment lol?
Anyway, you should put your calculation inside a While loop and then execute them if the win.getMouse() is inside the rectangle object. Then you should do the same for the "Quit" with a nested While loop.
To reiterate,
While True:
p = win.getMouse()
if p is inside rectangle:
//Calculate BMI
Then another nested *while* loop
I would also recommend taking a look at your decision structure and modifying the if, elif, elses because this has some redundancy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.