Pyglet hello world 示例在按下某个键之前不显示标签 [英] Pyglet hello world example doesn't show label until a key is pressed

查看:38
本文介绍了Pyglet hello world 示例在按下某个键之前不显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import pyglet

window = pyglet.window.Window()
label = pyglet.text.Label("Hello World!",
                            font_name="Times New Roman",
                            color=(255,255,255,255),
                            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()

pyglet.app.run()

此代码取自 https 上的 pyglet 教程://pyglet.readthedocs.io/en/pyglet-1.2-maintenance/programming_guide/quickstart.html 但运行时它不会绘制标签,直到按下任何键.我添加了颜色,因为我认为文本可能默认为黑色.

This code is taken from the pyglet tutorial at https://pyglet.readthedocs.io/en/pyglet-1.2-maintenance/programming_guide/quickstart.html but when run it doesn't draw the label until any key is pressed. I added the color as I thought the text may have defaulted to black.

我是否遗漏了一些非常明显的关于为什么会发生这种行为的信息?

Am I missing something really obvious as to why this behaviour is happening?

好的,我的记忆被评论唤醒了,我安装了 MS Fonts,它现在可以在 python 2.x 中运行,但我仍然需要按一个键才能看到 python 3 中的文本.也许字体是一个红鲱鱼,与 python 3 有一些不兼容.

OK, having had my memory jogged by the comments, I installed the MS Fonts and it now works in python 2.x but I still need to press a key to see the text in python 3. Maybe the font thing is a red-herring and there is some incompatibility with python 3.

推荐答案

如评论中所述.
互联网上的大多数示例(甚至大多数指南)都假定使用的是 Windows 平台.

As mentioned in the comments.
Most examples found on the internet (even most guides) assume a Windows platform.

如果有一个带有 Windows 字体的 font= 声明,但您运行的是 Linux,请确保您安装了正确的字体或恢复为您已安装的字体.

If there's a font= declaration with a Windows font but you're running Linux, make sure you got the proper fonts installed or revert to a font you've got installed.

$ fc-list

同样不声明字体也可以:

Also not declaring a font will work too:

label = pyglet.text.Label("Hello World!",
                            color=(255,255,255,255),
                            font_size=36,
                            x=window.width//2, y=window.height//2,
                            anchor_x="center", anchor_y="center")

因为 Pyglet 将默认为 sans-serif:

Because Pyglet will default to sans-serif:

如果你不是特别关心使用哪种字体,只需要显示一些可读的文本,你可以指定 None 作为系列名称,这将加载默认的无衬线字体(Mac OS X 上的 Helvetica,Arial 上视窗 XP)

If you do not particularly care which font is used, and just need to display some readable text, you can specify None as the family name, which will load a default sans-serif font (Helvetica on Mac OS X, Arial on Windows XP)

您的问题可能是您在绘制某些内容后没有手动更新屏幕.通常当你按下一个键时,它会强制一个 window.flip(),这基本上意味着更新屏幕内容.
window.clear() 也会触发此行为,但 x.draw() 不会.为什么?好吧,计算机图形学中需要时间的并不是计算,而是将它们更新到屏幕上需要时间.. .draw() 不更新屏幕,它只是放置图形缓冲区(页面")中的内容,您决定何时翻转页面并在屏幕上显示新缓冲区.

Your issue is probably that you don't manually update the screen after you drew something. Normally when you press a key, it forces a window.flip(), which basically means update the screen content.
window.clear() also triggers this behavior, but x.draw() does not. Why? Well the thing that takes time in computer graphics is not really the calculations, it's the updating them to the screen that takes time.. There for .draw() doesn't update the screen, it just puts the stuff in the graphical buffer ("page"), you decide when to flip the page and show the new buffer on the screen.

试试这个:

@window.event
def on_draw():
        window.clear()
        label.draw()
        window.flip()

这可能是一个矫枉过正的解决方案,但它可能会解决问题.
这会覆盖 pyglet 的默认循环和默认绘制行为,它也是我在 pyglet 项目中使用最多的类之一,因为它让我可以选择创建自己的框架.

This might be a overkill solution, but it will probably solve the problem.
This overrides the default loop of pyglet and the default draw behavior, it's also one of the classes i use the most in my pyglet projects since it gives me the option to create my own framework.

import pyglet

class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(vsync = False)
        self.sprites = {}
        self.sprites['testlabel'] = label = pyglet.text.Label("Hello World!",
                        color=(255,255,255,255),
                        font_size=36,
                        x=self.width//2, y=self.height//2, #self here, being pyglet.window.Window that we've inherited and instanciated with super().
                        anchor_x="center", anchor_y="center")
        self.alive = 1

    def on_draw(self):
        self.render()

    def render(self):
        self.clear()
        for sprite_name, sprite_obj in self.sprites.items():
            sprite_obj.draw()
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()
            # This is very important, this queries (and empties)
            # the pyglet event queue, if this queue isn't cleared
            # pyglet will hang because it can't input more events,
            # and a full buffer is a bad buffer, so we **NEED** this!
            event = self.dispatch_events()

win = Window()
win.run()

这是非常基本的,但您可以添加更多精灵、对象并在 def render(self): 中渲染它们.

It's extremely basic, but you can add more sprites, objects and render them in def render(self):.

这篇关于Pyglet hello world 示例在按下某个键之前不显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆