This tutorial will show you how to utilise events.

All event methods are recognised by the naming convention of having on_ prepended to their names. You will find these methods documented within each widget, for example the Button widget has an on_click() method; this is run whenever the button is clicked via mouse or keyboard.


The event methods, if left unchanged, will emit a Pygame event. All events use the GUI constant for their type attribute.

Try adding this code to the event loop of your program or the getting started code to see all the events being emitted by the toolkit:

if event.type == GUI:
    print event

All of these events also have these extra attributes:


The class of the widget that emitted the event.


The instance of the widget that emitted the event.


A string indicating what this event represents.

Some events will have additional attributes and are documented with their event methods.


If you prefer to use callbacks rather than handling events, this can be easily accomplished by overloading the method.

You could do this using a subclass:

class MyButton(sgc.Button):
    def on_click(self):
        print "Overloading through inheritance."

Or, you could simply reassign a function:

def foo():
    print "Replace through assignment."

my_button.on_click = foo

Or, you can pass the function into config() to assign it:

def foo():
    print "Assign through config."


Using both events and callbacks

Because using a callback will suppress the event, if you want to use both callbacks and events for a widget you must call the original method in your callback.

class MyButton(sgc.Button):
    def on_click(self):
        print "Using a callback and sending an event."

Table Of Contents

Previous topic

Getting Started

Next topic