Hi. I'm new to pyglet and having a little difficulty understanding how 
pyglet.app.run() updates multiple windows, particularly windows that are 
not the active window. 

I am trying to write a simple program that does the following.

   1. Creates 2 windows that are initially colored all white. 
   2. If the user presses the key "1", it should toggle the first window 
   between white and black and print "1" to the console.
   3. If the user presses the key "2", it should toggle the second window 
   between white and black and print "2" to the console

The toggling behavior should occur for either window regardless of what 
window is active (i.e., if window 1 is active and the user presses "2", the 
color of window 2 should toggle)

import pyglet
from pyglet.window import key
from pyglet.gl import *

class ToggleWindow(pyglet.window.Window):

    def __init__(self):
        super(ToggleWindow, self).__init__()
        self.color = 1.0

    def toggle(self):
        if self.color == 1.0:
            self.color = 0.0
        else:
            self.color = 1.0
      
    def on_draw(self):
        self.switch_to()
        
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glColor3f(self.color,self.color,self.color)
        glBegin(GL_QUADS)
        glVertex2f(0,0)
        glVertex2f(self.width,0)
        glVertex2f(self.width,self.height)
        glVertex2f(0,self.height)
        glEnd()

def main():

    window1 = ToggleWindow()
    window2 = ToggleWindow()

    @window1.event
    @window2.event
    def on_key_press(symbol, modifiers):
        if symbol == key._1:
            print(1)
            window1.toggle()
        elif symbol == key._2:
            print(2)
            window2.toggle()
        else:
            print(symbol)
    
    pyglet.app.run()

            
if __name__ == "__main__":
    main()


The problem is that only the active window color will toggle. Is there some 
event I have to dispatch to get the render loop to redraw an inactive 
window? I thought maybe the "switch_to()" method would do this but I guess 
it only changes the opengl context?

I can get the program to work as intended if I replace pyglet.app.run() 
with a manual render loop like I might do in SDL, but this is obviously the 
sort of thing pyglet is designed to avoid.

while True:
        pyglet.clock.tick()

        for window in pyglet.app.windows:
            window.switch_to()
            window.dispatch_events()
            window.dispatch_event('on_draw')
            window.flip()


 
Any thoughts? I am on linux if it is relevant.

Thanks in advance for any help.


-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to