我正在尝试使用pyglet制作一个简单的GUI。
这里是我的代码:
button_texture = pyglet.image.load('button.png')
button = pyglet.sprite.Sprite(button_texture, x=135, y=window.height-65)
def on_mouse_press(x, y, button, modifiers):
if x > button and x < (button + button_texture.width):
if y > button and y < (button + button_texture.height):
run_program()

问题
"button.png“显示为红色框,里面有"Click”。并且应该启动run_program()。但目前,左下角的黄色是我必须单击以启动run_program()的位置。
发布于 2014-01-20 12:44:32
您正在将按钮(键代码)与X/Y坐标进行比较。这是因为函数参数button隐藏了全局变量。另外,您应该使用按钮x、y、width和height属性。
button_texture = pyglet.image.load('button.png')
button_sprite = pyglet.sprite.Sprite(button_texture, x=135, y=window.height-65)
def on_mouse_press(x, y, button, modifiers):
if x > button_sprite.x and x < (button_sprite.x + button_sprite.width):
if y > button_sprite.y and y < (button_sprite.y + button_sprite.height):
run_program()为了避免名称冲突,我将全局变量button重命名为button_sprite。
https://stackoverflow.com/questions/21234381
复制相似问题