你只需要得到win.getMouse()
返回的點,並確保x和y值在邊界內。我這樣做,下面的inside
功能,然後用這個布爾值顯示「Y」或「N」的窗口上
from graphics import *
def inside(test_Point, P1, P2):
'''
determines if the test_Point is inside
the rectangle with P1 and P2 at opposite corners
assumes P1 is upper left and P2 is lower right
'''
tX = test_Point.getX()
tY = test_Point.getY()
# first the x value must be in bounds
t1 = (P1.getX() <= tX) and (tX <= P2.getX())
if not t1:
return False
else:
return (P2.getY() <= tY) and (tY <= P1.getY())
win = GraphWin("Box", 600, 600)
yes_box = Rectangle(Point(200, 150), Point(350, 50))
yes_box.setOutline('blue')
yes_box.setWidth(1)
yes_box.draw(win)
# where was the mouse clicked?
t = win.getMouse()
# is that inside the box?
if inside(t, yes_box.getP1(), yes_box.getP2()):
text = 'y'
else:
text = 'n'
# draw the 'y' or 'n' on the screen
testText = Text(Point(200,300), text)
testText.draw(win)
exitText = Text(Point(200,350), 'Click anywhere to quit')
exitText.draw(win)
win.getMouse()
win.close()
不知道的框架,但不能你的鼠標當前位置和比較它到你用於矩形的座標? –
看,我不知道我會如何去做這件事。 – NDIrishman23
所以實際的問題是「如何在這個圖形框架中獲得鼠標指針的位置」? –