繁体   English   中英

在Zelle的图形中创建一个按钮(从图像)

[英]Creating a Button (from image) in Zelle's Graphics

我有一个开始按钮图像,试图将其转换为程序中的按钮。 但是,我认为我做错了数学或显然有错,因为它不起作用。 基本上,我要尝试的是如果该人单击该按钮,它将启动一个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是我要创建按钮的图像。 hour是其他代码中声明的变量。

您正在将苹果与橙子进行比较。 这行:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM