忽略其他鼠标点击,Graphics.py Python
Ignore other Mouse Clicks, Graphics.py Python
所以我有代码,如果用户单击显示“计算”的矩形或按钮,它会正确执行所有操作。但是,我如何编写一个代码来忽略所有其他点击,直到它们在矩形内点击或“计算”我尝试使用 while 循环但我无法让它工作。我想知道是否有人能够帮助我解决我的问题。
这是我的代码:
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()
这是凯特教授的作业吗?
无论如何,您应该将计算放在 While 循环中,然后在 win.getMouse() 位于矩形对象内部时执行它们。然后你应该用嵌套的 While 循环对“退出”做同样的事情。
重申一下,
While True:
p = win.getMouse()
if p is inside rectangle:
//Calculate BMI
Then another nested *while* loop
我还建议您查看您的决策结构并修改 if、elif 和 else,因为这有一些冗余。
所以我有代码,如果用户单击显示“计算”的矩形或按钮,它会正确执行所有操作。但是,我如何编写一个代码来忽略所有其他点击,直到它们在矩形内点击或“计算”我尝试使用 while 循环但我无法让它工作。我想知道是否有人能够帮助我解决我的问题。
这是我的代码:
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()
这是凯特教授的作业吗?
无论如何,您应该将计算放在 While 循环中,然后在 win.getMouse() 位于矩形对象内部时执行它们。然后你应该用嵌套的 While 循环对“退出”做同样的事情。
重申一下,
While True:
p = win.getMouse()
if p is inside rectangle:
//Calculate BMI
Then another nested *while* loop
我还建议您查看您的决策结构并修改 if、elif 和 else,因为这有一些冗余。