2013-03-28 2 views
1

특성으로 Pyglet 창이있는 주 개체가 있습니다. Pylget의 window 클래스에는 푸시 핸들러 (push handlers)라는 메서드가 있습니다.이 메서드를 사용하면 메서드를 이벤트 스택에 푸시 할 수 있습니다. 다음 코드는 작동 :다른 개체의 처리기를 창에 추가하는 방법

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()) 

위의 코드는 기본 크기에서 새로운 윈도우를 생성하고 그것에 on_mouse_press()와 on_draw 이벤트 핸들러를 첨부합니다. 잘 작동하지만 다른 클래스의 push_handlers() 메서드를 호출하려고해도 작동하지 않습니다.

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() 

위의 코드는 새 창을 생성하지만 메뉴 클래스의 핸들러는 첨부하지 않습니다. 이것에 대한 이유가 있거나 사용할 수있는 해결 방법이 있습니까? 감사!

답변

0

pyglet.app.run()을 호출하면 pyglet 루프가 시작되고 pyglet 윈도우가 닫힐 때까지 돌아 오지 않으므로 mgenu()는 pyglet 루프가 끝날 때만 호출됩니다. Main에서 pyglet.app.run 행을 제거하고 다음과 같이 호출하면 다음과 같이 호출합니다.

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

작동합니다.

관련 문제