Screenshot from Pyglet [Fix'd]

In pyglet docs, I found:

The following example shows how to capture a screenshot of your application window: pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png')

However, when using this, everything will stop until I click the mouse button. Is there any other way to get the contents of the screen in Pyglet or force it back into the event loop?

EDIT: I found that there is actually a slight delay (0.2 seconds ~), but nothing else. In fact, this is somehow connected with the F10 key, which stops the pinglet. > _>

I cannot close or delete, as there is open generosity.

+6
python events screenshot pyglet
source share
2 answers

Ok, here is a complete working example in pyglet. It displays the text "hello world", which takes a random walk around the window and resets the screenshot (using the same line of code that you sent) every time you press a key.

 import pyglet, random window = pyglet.window.Window() label = pyglet.text.Label('Hello, world', font_name='Times New Roman', font_size=36, x=window.width//2, y=window.height//2, anchor_x='center', anchor_y='center') @window.event def on_draw(): window.clear() label.draw() @window.event def on_key_press(symbol, modifiers): pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png') def update(dt): label.x += random.randint(-10, 10) label.y += random.randint(-10, 10) pyglet.clock.schedule_interval(update, 0.1) pyglet.app.run() 

Taking a screenshot does not stop the event loop. The event loop in pyglet is just lazy and tries to do as little work as possible. You need to plan the execution of the function if you want everything to continue. Otherwise, he will wait for the event to which the listener is connected. (Your code should listen for the mouse event, so it resumes when the mouse is clicked.)

Short answer, I suspect you will need pyglet.clock.schedule_interval(...) .

+8
source share

If you are on a Windows platform, you can create a screenshot from PIL: http://effbot.org/imagingbook/imagegrab.htm

(PIL is cross-platform, with the exception of one specific method.)

As for the pyglet method, can you post a bit more source code? It seems strange that this will break the cycle of events. If this is true, maybe you can transfer this single method call to a thread?

+1
source share

All Articles