繁体   English   中英

忽略其他鼠标点击,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() 在矩形 object 内则执行它们。然后您应该使用嵌套的While循环对“退出”执行相同的操作。

重申一下,

While True:
     p = win.getMouse()
     if p is inside rectangle:
          //Calculate BMI
          Then another nested *while* loop

我还建议您查看您的决策结构并修改 if、elif 和 else,因为这有一些冗余。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM