Python Pyglet 鼠标事件不调用 on_draw() 也不在 window 中进行更改
Python Pyglet mouse events don't call on_draw() nor make changes in window
出于某种原因,我的程序发生的任何鼠标事件都不会使 window 中的图像消失或重新出现,尽管我的 on_key_press()
功能有效。
我尝试了状态标志并声明了一个新的图像资源,但它并没有改变 window 中的任何内容。
有没有办法让它工作?我应该恢复到以前的 pyglet 版本吗?如果有,是哪个版本?
这是我的程序代码;它以可见的测试图像运行,每次按下键或单击鼠标时,图像都会消失或重新出现:
import pyglet
window = pyglet.window.Window()
image = pyglet.resource.image('test.png')
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
state = True
@window.event
def on_draw():
print('on_draw() called')
window.clear()
if state:
image.blit(window.width // 2, window.height // 2)
@window.event
def on_mouse_press(x, y, button, modifiers):
global state, image
if button == pyglet.window.mouse.LEFT:
print('mouse press')
if state:
state = False
else:
state = True
@window.event
def on_key_press(symbol, modifiers):
global state
print('key press')
if state:
state = False
else:
state = True
pyglet.app.run()
谢谢!
编辑:我的 python 版本是 3.7.2,我的 pyglet 版本是 1.4.7,我使用 pycharm,如果事实似乎是...
看来是装饰器函数的问题。
正如 Torxed 所建议的,不要装饰 on_mouse_press
,而是用您自己的函数声明替换 window 对象的 on_mouse_press
函数:
import pyglet
image = pyglet.resource.image('test.png')
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
state = True
def on_draw():
print('on_draw() called')
window.clear()
if state:
image.blit(window.width // 2, window.height // 2)
def on_mouse_press(x, y, button, modifiers):
global state
print('mouse pressed')
if state:
state = False
else:
state = True
window = pyglet.window.Window()
window.on_draw = on_draw
window.on_mouse_press = on_mouse_press
pyglet.app.run()
否则,创建 Window
对象的子类并用您自己的声明覆盖 on_mouse_press
函数:
import pyglet
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(800, 600)
self.image = pyglet.resource.image('test.png')
self.image = pyglet.resource.image('test.png')
self.image.anchor_x = self.image.width // 2
self.image.anchor_y = self.image.height // 2
self.state = True
def on_draw(self):
print('on_draw() called')
window.clear()
if self.state:
self.image.blit(self.width // 2, self.height // 2)
def on_mouse_press(self, x, y, button, modifiers):
print('mouse pressed')
if self.state:
self.state = False
else:
self.state = True
window = Window()
pyglet.app.run()
出于某种原因,我的程序发生的任何鼠标事件都不会使 window 中的图像消失或重新出现,尽管我的 on_key_press()
功能有效。
我尝试了状态标志并声明了一个新的图像资源,但它并没有改变 window 中的任何内容。
有没有办法让它工作?我应该恢复到以前的 pyglet 版本吗?如果有,是哪个版本?
这是我的程序代码;它以可见的测试图像运行,每次按下键或单击鼠标时,图像都会消失或重新出现:
import pyglet
window = pyglet.window.Window()
image = pyglet.resource.image('test.png')
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
state = True
@window.event
def on_draw():
print('on_draw() called')
window.clear()
if state:
image.blit(window.width // 2, window.height // 2)
@window.event
def on_mouse_press(x, y, button, modifiers):
global state, image
if button == pyglet.window.mouse.LEFT:
print('mouse press')
if state:
state = False
else:
state = True
@window.event
def on_key_press(symbol, modifiers):
global state
print('key press')
if state:
state = False
else:
state = True
pyglet.app.run()
谢谢!
编辑:我的 python 版本是 3.7.2,我的 pyglet 版本是 1.4.7,我使用 pycharm,如果事实似乎是...
看来是装饰器函数的问题。
正如 Torxed 所建议的,不要装饰 on_mouse_press
,而是用您自己的函数声明替换 window 对象的 on_mouse_press
函数:
import pyglet
image = pyglet.resource.image('test.png')
image.anchor_x = image.width // 2
image.anchor_y = image.height // 2
state = True
def on_draw():
print('on_draw() called')
window.clear()
if state:
image.blit(window.width // 2, window.height // 2)
def on_mouse_press(x, y, button, modifiers):
global state
print('mouse pressed')
if state:
state = False
else:
state = True
window = pyglet.window.Window()
window.on_draw = on_draw
window.on_mouse_press = on_mouse_press
pyglet.app.run()
否则,创建 Window
对象的子类并用您自己的声明覆盖 on_mouse_press
函数:
import pyglet
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(800, 600)
self.image = pyglet.resource.image('test.png')
self.image = pyglet.resource.image('test.png')
self.image.anchor_x = self.image.width // 2
self.image.anchor_y = self.image.height // 2
self.state = True
def on_draw(self):
print('on_draw() called')
window.clear()
if self.state:
self.image.blit(self.width // 2, self.height // 2)
def on_mouse_press(self, x, y, button, modifiers):
print('mouse pressed')
if self.state:
self.state = False
else:
self.state = True
window = Window()
pyglet.app.run()