Search code examples
pythonwindowpyglet

Make Window In PyGlet


I started to learn PyGlet (Python Framework for games). I made default window, but when I slide it in the edge of the monitor, it looks something like this:enter image description here

import pyglet

window = pyglet.window.Window(800, 600, "PyGlet Window")

pyglet.app.run()

What's the problem? Thanks for all support!


Solution

  • This has to do with buffers and how windows handle windows that are outside of the screen. IIRC they get optimized away and the buffer or area of the window will not be automatically cleared. And if you don't clear it or no event triggers a re-render, whatever is stuck in the buffer will be what's visible.

    Easiest solution around this is to manually call .clear() the window object:

    import pyglet
    
    window = pyglet.window.Window(800, 600, "PyGlet Window")
    
    def callback(dt):
        print('%f seconds since last callback' % dt)
    
    @window.event
    def on_draw():
        window.clear()
        window.flip() # Not necessary, but probably doesn't hurt
    
    pyglet.clock.schedule_interval(callback, 0.5)   # called twice a second
    pyglet.app.run()
    

    Any time you cause an event, the draw function should be triggered, and if your on_draw contains window.clear() it should clear any old artifacts.