2016-11-17 71 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() 

這個特定的圖標顯示爲一個圓圈,您可以點擊區域包括其矩形邊界框,其中一些在圓外。可以將點擊次數限制爲可見圖像,但需要更多工作。