使用 Pyglet 截屏 [修复]

发布于 2024-10-17 08:39:33 字数 364 浏览 2 评论 0原文

在 pyglet 文档中,我发现:

以下示例展示了如何 获取您的应用程序的屏幕截图 窗户: pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png')

但是,当使用这个时,一切都会停止,直到我单击鼠标。是否有另一种方法可以获取 Pyglet 中的屏幕内容,或强制其返回事件循环?

编辑:我发现实际上有一个短暂的延迟(0.2秒〜),但没有其他。实际上,这与停止 pyglet 的 F10 键有关。 >>_>>

我无法关闭或删除,因为有悬赏。

In the pyglet docs, I found:

The following example shows how to
grab 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. Is there another way to get the screen contents in Pyglet, or to force it back into the event loop?

EDIT: I have found that actually there is a short delay (0.2 seconds~), but nothing else. Actually it is something to do with the F10 key that stops pyglet. >_>

I cannot close or delete since there is an open bounty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2024-10-24 08:39:33

好的,这是 pyglet 中的完整工作示例。它会显示文本“hello world”在窗口中随机走动,并在每次按下按键时转储屏幕截图(使用与您发布的完全相同的代码行)。

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()

截取屏幕截图不会停止事件循环。 pyglet 中的事件循环只是懒惰的,并尝试做尽可能少的工作。如果您希望事情继续自行发生,您需要安排一个函数重复运行。否则,它将等待附加侦听器的事件发生。 (您的代码必须侦听鼠标事件,这就是为什么当您单击鼠标时它会恢复工作。)

简短的回答,我怀疑您需要的修复是 pyglet.clock.schedule_interval(...)。

Okay, here is a complete working example in pyglet. It shows the text "hello world" taking a random walk around the window and dumps the screenshot (using the exact same line of code you posted) 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 the screenshot doesn't halt the event loop. The event loop in pyglet is just lazy, and tries to do as little work as possible. You need to schedule a function to run repeatedly if you want things to keep happening on their own. Otherwise, it'll wait for an event that has a listener attached to happen. (Your code must be listening for the mouse event, which is why it resumes work when you click the mouse.)

Short answer, I suspect the fix you need is pyglet.clock.schedule_interval(...).

女中豪杰 2024-10-24 08:39:33

如果您恰好在 Windows 平台上,可以使用 PIL 创建屏幕截图: http://effbot.org /imagingbook/imagegrab.htm

(PIL 是跨平台的,除了那个特定的方法。)

至于 pyglet 的方法,你能多发布一点源代码吗?这会打破事件循环,这似乎很奇怪。如果确实如此,也许您可​​以将单个方法调用包装在线程中?

If you happen to be on a windows platform, you can create a screenshot with PIL: http://effbot.org/imagingbook/imagegrab.htm

(PIL is cross platform except for that one particular method.)

As for pyglet's method, can you post a little more source code? It seems bizarre that that would break the event loop. If that really is the case, perhaps you could wrap that single method call in a thread?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文