首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pyglet on_mouse_press

pyglet on_mouse_press
EN

Stack Overflow用户
提问于 2014-01-20 12:38:26
回答 1查看 2.2K关注 0票数 1

我正在尝试使用pyglet制作一个简单的GUI。

这里是我的代码:

代码语言:javascript
复制
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()的位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-20 12:44:32

您正在将按钮(键代码)与X/Y坐标进行比较。这是因为函数参数button隐藏了全局变量。另外,您应该使用按钮xywidthheight属性。

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21234381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档