使用 Pyglet 截屏 [修复]
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这是 pyglet 中的完整工作示例。它会显示文本“hello world”在窗口中随机走动,并在每次按下按键时转储屏幕截图(使用与您发布的完全相同的代码行)。
截取屏幕截图不会停止事件循环。 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.
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(...)
.如果您恰好在 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?