首页 > 解决方案 > 我想使用 pyglet 在新窗口中显示五个数字

问题描述

我一直在尝试每秒显示从 0 到 4 的五个数字。

import pyglet
import time

window = pyglet.window.Window()

@window.event
def on_draw():
    for n in range(0,5):
        window.clear()
        label = pyglet.text.Label(str(n),font_name='Times New Roman',font_size=72,x=window.width//2, y=window.height//2,anchor_x='center', anchor_y='center')
        label.draw()
        print(n)
        time.sleep(1)

pyglet.app.run()

它没有遇到任何错误,尽管它是一个空白屏幕几秒钟,之后它只显示第四个。

标签: pythonpyglet

解决方案


我将从文档开始并查看简单的文本示例。

https://pyglet.readthedocs.io/en/latest/programming_guide/text.html

on_draw没有被正确使用。

然后对于每 1 秒的调度更改,请查看时钟调度示例。

https://pyglet.readthedocs.io/en/latest/modules/clock.html#scheduling


推荐阅读