为什么我的 window 不适用于 on_draw?

why does my window doesn't work for on_draw?

我正在观看一个关于 pyglet 的视频,我试图创建一个三角形:

import pyglet
from pyglet.gl import *

class mywindow(pyglet.window.Window):
    def __init__(self, *args,**kwargs):
        super().__init__(*args,**kwargs)
        self.set_minimum_size(300,300)
        
window = mywindow(300,300,"deneme", True)

def on_draw():
    glBegin(GL_TRIANGLE)
    glColor3b(255,0,0)
    glVertex2f(-1,0)
    glColor3b(0,255,0)
    glVertex2f(1,0)
    glColor3b(0,0,255)
    glVertex2f(0,1)

window.on_draw()
pyglet.app.run()

    

当我运行这段代码时;我收到此错误:

AttributeError: 'mywindow' object has no attribute 'on_draw'

知道如何解决这个问题吗?

In [1]: from pyglet.gl import *
   ...:
   ...: window = pyglet.window.Window()
   ...:
   ...: vlist = pyglet.graphics.vertex_list(3, ('v2f', [0,0, 400,50, 200,300]))
   ...:
   ...: @window.event
   ...: def on_draw():
   ...:     glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
   ...:     glColor3f(1,0,0)
   ...:     vlist.draw(GL_TRIANGLES)
   ...:
   ...: pyglet.app.run()

输出:

on_draw 必须是 class mywindow 的方法而不是函数。不要自己调用 on_draw,因为它会在需要更新 window 时自动调用。
on_draw 开始时,您必须通过(参见 Windowing)清除显示。
OpenGL 即时模式 glBegin/glEnd 序列必须以 glEnd. The primitive type is GL_TRIANGLES rather than GL_TRIANGLE. If you want to specify the colors in range [0, 255], the you've to use glColor3ub(无符号字节)而不是 glColor3b(有符号字节)结束。
您必须在 on_resize 事件中通过 glViewport 设置可调整大小 window 的视口矩形。

看例子:

import pyglet
from pyglet.gl import *

class mywindow(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_minimum_size(300,300)

    def on_draw(self):

        self.clear()

        glBegin(GL_TRIANGLES)
        glColor3ub(255, 0, 0)
        glVertex2f(-1, 0)
        glColor3ub(0, 255, 0)
        glVertex2f(1, 0)
        glColor3ub(0, 0, 255)
        glVertex2f(0, 1)
        glEnd()

    def on_resize(self, width, height):
        glViewport(0, 0, width, height)

window = mywindow(300,300,"deneme", True)
pyglet.app.run()