Search code examples
pythonpython-2.7pyglet

Locking the size of a resizable window to the original aspect ratio of the window in Pyglet


I'm making an image viewer, and I want it to resize only to scale factors of the original image size. In short, on resize, I want the entire image visible and no whitespace in the window. Is there any way to do this in pyglet? I've tried using the window.set_size() function, but I can't exactly figure out how to word the code so that it works the way I want it to. Any help is appreciated.


Solution

  • Although this looks a little hackish for me, but at the moment I don't think of any other possibilities to solve you problem. Anyway, this is working:

    import pyglet
    
    window = pyglet.window.Window( resizable=True )
    
    @window.event
    def on_resize( width, height ):
        ratio = 9/16
        window.set_size( width, width*ratio )
    
    @window.event
    def on_draw():
        window.clear()
    
    pyglet.app.run()