2016-11-17 2 views
0

自分のプログラムのボタンに変えようとしているスタートボタンの画像があります。しかし、私はそれが動作していないので、私は間違っているか間違って何かを明らかにしていると私は信じています。基本的に、私がやっていることは、人がボタンをクリックすると、ifステートメントを開始するということです。何か案は?前もって感謝します!Zelleのグラフィックスのボタン(画像から)を作成する

#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は、私は、ボタンを作りたいイメージです。時間は他のコードで述べられている変数です。

答えて

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() 

この特定のアイコンが円として現れる、あなたがクリックできる領域はそのを含みいくつかは円の外側にある四角形の境界ボックスです。クリック数を表示画像と同じにすることは可能ですが、それ以上の作業が必要です。

関連する問題