0

プログラムでボタンに変えようとしているスタートボタンの画像があります。ただし、数学が間違っているか、明らかに機能していないため、何か間違っていると思います。基本的に、私がやろうとしているのは、人がボタンをクリックすると、if ステートメントが開始されるということです。何か案は?前もって感謝します!

    #Assigning Mouse x,y Values
mousePt = win.getMouse()
xValue = startImage.getHeight()
yValue = startImage.getWidth()

#Assigning Buttons
if mousePt <= xValue and mousePt <= yValue:
    hour = 2

startImage はボタンにしたい画像です。時間は、他のコードで記述された変数です。

4

1 に答える 1

0

あなたはリンゴとオレンジを比較しています。この行:

if mousePt <= xValue and mousePt <= yValue:

は、次のように言っているのとほぼ同じです。

if Point(123, 45) <= 64 and Point(123, 45) <= 64:

ポイントを幅や高さと比較しても意味がありません。幅と高さを画像の中心位置と組み合わせ、マウスの位置から X と Y の値を抽出する必要があります。

from graphics import *

win = GraphWin("Image Button", 400, 400)

imageCenter = Point(200, 200)
# 64 x 64 GIF image from http://www.iconsdb.com/icon-sets/web-2-green-icons/video-play-icon.html
startImage = Image(imageCenter, "video-play-64.gif")
startImage.draw(win)

imageWidth = startImage.getWidth()
imageHeight = startImage.getHeight()

imageLeft, imageRight = imageCenter.getX() - imageWidth/2, imageCenter.getX() + imageWidth/2
imageBottom, imageTop = imageCenter.getY() - imageHeight/2, imageCenter.getY() + imageHeight/2

start = False

while not start:
    # Obtain mouse Point(x, y) value
    mousePt = win.getMouse()

    # Test if x,y is inside image
    x, y = mousePt.getX(), mousePt.getY()

    if imageLeft < x < imageRight and imageBottom < y < imageTop:
        print("Bullseye!")
        break

win.close()

この特定のアイコンは円として表示され、クリックできる領域には長方形の境界ボックスが含まれ、その一部は円の外側にあります。クリックを正確に表示されている画像に制限することは可能ですが、それにはより多くの作業が必要です。

于 2017-01-13T20:42:43.383 に答える