Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用鼠标按python pyglet_Python_Pyglet - Fatal编程技术网

用鼠标按python pyglet

用鼠标按python pyglet,python,pyglet,Python,Pyglet,我正在尝试使用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_te

我正在尝试使用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\u texture=pyglet.image.load('button.png'))
按钮=pyglet.sprite.sprite(按钮纹理,x=135,y=window.height-65)
按鼠标上的def键(x、y、按钮、修改器):
如果x>按钮和x<(按钮+按钮\u纹理宽度):
如果y>按钮且y<(按钮+按钮\u纹理高度):
运行_程序()

问题


“button.png”显示为红色框,内有“Click”。并且应该启动run_program()。但目前,左下角的黄色是我必须单击以启动run_program()的位置

您正在将按钮(键代码)与X/Y坐标进行比较。这种情况会发生,因为函数参数
按钮
会遮挡全局变量。此外,还应使用按钮
x
y
宽度
高度
属性

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\u texture=pyglet.image.load('button.png'))
按钮精灵=pyglet.sprite.sprite(按钮精灵纹理,x=135,y=window.height-65)
按鼠标上的def键(x、y、按钮、修改器):
如果x>button\u sprite.x和x<(button\u sprite.x+button\u sprite.width):
如果y>button\u sprite.y和y<(button\u sprite.y+button\u sprite.height):
运行_程序()
我用
button\u sprite
重命名了全局变量
button
,以避免名称冲突