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

这个特定的图标显示为一个圆圈,您可以点击区域包括其矩形边界框,其中一些在圆外。可以将点击次数限制为可见图像,但需要更多工作。