Updating multiple windows in response to keyboard input

377 views
Skip to first unread message

Gregory Howland

unread,
Dec 9, 2016, 10:13:51 AM12/9/16
to pyglet-users
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.


Gregory Howland

unread,
Dec 9, 2016, 11:08:38 AM12/9/16
to pyglet-users
I was able to get it to work with pylget.app.run() by adding self.dispatch_event('on_draw') to the toggle method in my window class. Is this the proper approach?

Benjamin Moran

unread,
Dec 12, 2016, 9:39:52 AM12/12/16
to pyglet-users
I haven't really used pyglet with more than one window myself, but there shouldn't be anything wrong with doing it that way.
Reply all
Reply to author
Forward
0 new messages