6

I have a strange problem. When pyglet app starts it just draws 1-2 frames then freezes. on_draw event just stops occuring. But everytime I move mouse or press keys, on_draw event dispatches as well. In short I have to move mouse to make my pyglet application basically work.

This is actually happens in Windows. In Ubuntu with compiz I've to move mouse just once then application starts working normally.

This is my code example:

#!/usr/bin/env python

import pyglet

win = pyglet.window.Window(width=800, height=600)
label = pyglet.text.Label('Abc', x=5, y=5)

@win.event
def on_draw():
    win.clear()
    label.x += 1
    label.draw()

pyglet.app.run()

Here's a video explaining things.

Pavel Vorobyov
  • 773
  • 6
  • 10
  • Implementing manual event loop instead of `pyglet.app.run()` helps. Debugging shows that pyglet freezes in `select.select()` of EventLoop. I didn't look into that deeper – Pavel Vorobyov Jan 12 '14 at 11:17
  • If you have different behaviours in windows and ubuntu, then that is definitely a bug. Can you report it [here](https://code.google.com/p/pyglet/issues/list)? In mac I also have a different behavior: on mouse it does not change, in keyboard it changes. – Jorge Leitao Jan 13 '14 at 09:44
  • by the way, did you tried using the tip of Pyglet repository? – Jorge Leitao Jan 13 '14 at 09:45
  • Was this ever solved? I am having the same issue, and I don't see it listed in the bug tracker. – PoDuck Apr 08 '14 at 03:37

2 Answers2

5

I came across this last night while trying to figure out the same problem. I figured out what causes this.

I had used a decorator and put my updates in the on_draw method, and it would run okay for a little while, then it would freeze, only to start working again when I moved the mouse or hit the keyboard. I tried all sorts of tricks to figure it out, I finally thought that maybe things were just running too fast, and that putting them in a batch and letting pyglet decide when to update them would be better. It worked.

I also scheduled things so that they would run about twice as fast as my refresh rate, but not so fast it would bog anything down. This is more than enough for smooth animations.

needles_list = [gauges.speedometer.Needle(speedometer_data, needle, batch=batch, group=needles),
                gauges.tachometer.Needle(tachometer_data, needle, batch=batch, group=needles)]

def update(dt):
for needle in needles_list:
    needle.update(dt)

pyglet.clock.schedule_interval(update, 1/120.0)

gauges.speedometer.Needle and gauges.tachometer.Needle are subclasses of pyglet.sprite.Sprite, and I wrote an update method for each of them. I then called their draw method in on_draw as normal.

@window.event()
def on_draw():
    window.clear()
    batch.draw()

I know this question has been up for a while, and the asker may have given up already, but hopefully it will help anyone else that's having this problem.

PoDuck
  • 1,381
  • 15
  • 38
0

I had a similar problem of update events not getting called (using pyglet from Cygwin on Windows), and it turned out there was a bug:

https://groups.google.com/forum/#!msg/pyglet-users/Qp1HzPHcUEQ/A9AFddycLSAJ

I'm not certain it's the same problem you're having, but it seemed worth mentioning. I ended up hacking the file mentioned (pyglet/app/win32.py) by hand (setting self._polling = True around line 105, in Win32EventLoop._timer_func) and all my updates started flowing just fine.

JonathanZ
  • 1,046
  • 11
  • 20