pygame is
Simple DirectMedia Layer is
Site Swing



The Command Dispatch pattern is helpful when implementing GUI frameworks for your game.

Widgets are derived from the Node class, and implement event handlers, which are methods which are the event names, and have a prefix of 'EVT_'. Eg a Button class would typically implement a EVT_MouseButtonDown method which is called when a 'MouseButtonDown' event is posted to the Root node, and the Button node has the focus.

If the Button node received other events, and did not have handlers for those events, the event is passed up to the parent node.

An event handler method can explicitly pass the event onto the parent by calling self.dispatch_to_parent(event).

The end result of using the command dispatch pattern is a heirarchy of widgets, of which only one can have focus at a time, and which respond to events.

Leaf objects can be added into the widget tree to any node. A leaf node would typically be used to implement dynamic widget behaviours, and not overide the main widget event handlers. Eg, tooltips and hover effects would be created using leaf objects.

class Event(object):
    def __init__(self, name, kw={}): = name
        self.dict = dict(kw)
        self.method_name = 'EVT_%s' %
    def __repr__(self):
        return "<event>" % (, self.dict)
class Dispatcher(object):
    def add(self, other):
        if isinstance(other, Dispatcher):
            other.parent = self
            raise ValueError("%s is not a Dispatcher" % other)
    def remove(self, other):
        if isinstance(other, Dispatcher):
            if other.parent is self:
                del other.parent
                raise ValueError("%s is not my child." % other)
            raise ValueError("%s is not a Dispatcher" % other)
    def dispatch(self, event):
        method = getattr(self, event.method_name, None)
        if method is not None: 
            return method(event)
    def dispatch_to_parent(self, event):
        parent = getattr(self, 'parent', None)
        if parent is not None:
            dispatch = getattr(parent, 'dispatch', None)
            if dispatch is not None: 
                return dispatch(event)
class Root(Dispatcher):
    def __init__(self):
        self.focused_node = self
    def focus(self, node):
        if node is self.focused_node: return
        print 'focusing on', node
        self.focused_node = node
    def blur(self, node):
        self.focused_node = self
    def post(self, event_name, kw):
        event = Event(event_name, kw)
        return self.focused_node.dispatch(event)
if __name__ == "__main__":
    class Button(Dispatcher):
        def EVT_KeyDown(self, event):
            return self
    #create the root node, which is the reciever of all events.        
    r = Root()
    #create a button node
    b = Button()
    #add the button node to the application
    #give the button node focus, it will now receive events
    #post a KeyDown event, which the button will recieve
    #post a Quit event, which the button will not receive. 
    #this event will be passed onto the button parent
    #the return value of the post method is the node which handled the event

our projects welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
recent releases
Nov 17, 2015

Nov 12, 2015

Nov 11, 2015

Nov 8, 2015

Oct 16, 2015

Oct 9, 2015

Sep 11, 2015

Sep 6, 2015

Aug 26, 2015

Aug 25, 2015

... more!
for pygame related questions, comments, and suggestions, please see help (lists, irc)