The Python Way of Handling Events

The following brief interactive session highlights the Python way of handling events:

>>> def closing(event):
...     print "windowClosing"
...
>>> def closed(event):
...     print "windowClosed"
...
>>> def open(event):
...     print "windowOpened"
...

Import a JFrame, create an instance of it, and configure the instance.

>>> from javax.swing import JFrame
>>> frame = JFrame()
>>> frame.title = "JFrame frame"
>>> frame.size = 200,200

Set the event properties of the frame to the functions created.

>>> frame.windowClosing = closing
>>> frame.windowClosed = closed
>>> frame.windowOpened = open

Notice that the event properties windowClosing, windowClosed, and windowOpened correspond to the methods in the java.awt.WindowListener ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.