Question

I have a main object with a Pyglet window as an attribute. Pylget's window class has a method called push handlers, which lets me push methods to the event stack. The following code works:

import pyglet

class main:
    win = None
    gameItems = {}

    def __init__(self, window):
        self.win = window
        gameItem.win = self.win
        self.gameItems["menu"] = menu()
        self.gameItems["menu"].add()
        pyglet.app.run()

class gameItem:
    win = None

    def add(self):
        self.win.push_handlers(self)

class menu(gameItem): ##I actually have multiple objects inheriting from gameItem, this is just one of them.
    def on_mouse_press(self, x, y, button, modifier):
        '''on_mouse_press() is an accepted handler for the window object.'''
        print(x)
        print(y)

    def on_draw(self):
        '''With a quick draw function, so I can see immediately
        that the handlers are getting pushed.'''
        pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2i', (256,350,772,350,772,450,256,450)))

m = main(pyglet.window.Window())

The above code will spawn a new window at the default size and attach the on_mouse_press() and on_draw event handlers to it. That works well and good - however, trying to call on the push_handlers() method in other classes doesn't seem to work.

import pyglet

class Main:
    win = None
    gameItems = {}

    def __init__(self, window):
        self.win = window
        GameItem.game = self
        GameItem.win = self.win
        self.gameItems["main"] = MainMenu()
        pyglet.app.run()

    def menu(self):
        self.gameItems["main"].add()

class GameItem:
    win = None

    def add(self):
        self.win.push_handlers(self)

class MainMenu(GameItem): ##I actually have multiple objects inheriting from gameItem, this is just one of them.
    def on_mouse_press(self, x, y, button, modifier):
        '''on_mouse_press() is an accepted handler for the window object.'''
        print(x)
        print(y)

    def on_draw(self):
        '''With a quick draw function, so I can see immediately
        that the handlers are getting pushed.'''
        pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2i', (256,350,772,350,772,450,256,450)))

m = Main(pyglet.window.Window(width=1024, height=768))
m.menu()

The above code spawns a new window, but it doesn't attach the menu class's handlers. Is there a reason for this, or a workaround I can use? Thanks!

Was it helpful?

Solution

When you call pyglet.app.run(), you enter the pyglet loop and it doesn't come back until the pyglet window is closed, so your m.menu() is called only when the pyglet loop ends. If you remove the pyglet.app.run line from Main and call it like this:

m = Main(pyglet.window.Window(width=1024, height=768))
m.menu()
pyglet.app.run()
print "Window is closed now."

It works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top