Search code examples
pythonpython-3.xpython-3.7pyglet

Pyglet New Version Display Only Black


So far I have always used Pyglet with subclassing window. In practice like this:

import pyglet
from pyglet import *
from pyglet.gl import *

class test(pyglet.window.Window):

    old_set = 1024, 576
    new_set = old_set

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.video_set()

        self.label = pyglet.text.Label("TEST", font_name='Times New Roman', font_size=36, x=0, y=0, anchor_x='center', anchor_y='center')

    def video_set(self):
        self.platform = pyglet.window.get_platform()
        self.default_display = self.platform.get_default_display()
        self.default_screen = self.default_display.get_default_screen()

        self.set_size(self.new_set[0], self.new_set[1])
        self.location = self.default_screen.width // 2 - self.new_set[0] // 2, self.default_screen.height // 2 - self.new_set[1] // 2
        self.set_location(self.location[0], self.location[1])

        self.set_caption("Test")

        self.set_fullscreen(False)

    def update(self, dt):
        pass

    def draw(self):
        self.clear()
        self.label.draw()

if __name__ == "__main__":
    t = test()
    pyglet.clock.schedule_interval(t.update, 1 / 120)
    pyglet.app.run()

Now I updated Pyglet a few days ago and now this script no longer works. By searching, I found that pyglet.window.get_platform() is deprecated and pyglet.canvas.Display() or pyglet.canvas.get_display() should be used instead. So I tested, both as it was said, and as I found in other answers, but in any case I get black screen with nothing displayed. Whether it is a label, a sprite or whatever. So, what am I doing wrong in this new version? Thanks for your help.


Solution

  • So, I doubt that this code worked before - but I'll give you the benefit of the doubt.
    Because in this case I think it's because you forgot on_ before draw(). Try out the code below and see if it works, if that's the case, I was on to something :)

    import pyglet
    from pyglet import *
    from pyglet.gl import *
    
    class test(pyglet.window.Window):
    
        old_set = 1024, 576
        new_set = old_set
    
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.video_set()
    
            self.label = pyglet.text.Label("TEST", font_name='Times New Roman', font_size=36, x=0, y=0, anchor_x='center', anchor_y='center')
    
        def video_set(self):
            self.default_display = pyglet.canvas.Display()
            self.default_screen = self.default_display.get_default_screen()
    
            self.set_size(self.new_set[0], self.new_set[1])
            self.location = self.default_screen.width // 2 - self.new_set[0] // 2, self.default_screen.height // 2 - self.new_set[1] // 2
            self.set_location(self.location[0], self.location[1])
    
            self.set_caption("Test")
    
            self.set_fullscreen(False)
    
        def update(self, dt):
            pass
    
        def on_draw(self):
            self.clear()
            self.label.draw()
    
    if __name__ == "__main__":
        t = test()
        pyglet.clock.schedule_interval(t.update, 1 / 120)
        pyglet.app.run()