Pythons Window EventListener

The following example illustrates the same functionality as in the module, except that everything is written using Python event properties and handlers instead of the more Java-centric way of doing things. Read the code, and see if you can pick out the differences.

from java.awt import Frame from javax.swing import JFrame def windowOpened(windowEvent):

handleEvent("windowOpened",windowEvent) def windowClosed(windowEvent):

handleEvent("windowClosed", windowEvent) def windowClosing(windowEvent):

handleEvent("windowClosing",windowEvent) def WindowActivated(windowEvent):

handleEvent("WindowActivated", windowEvent) def windowDeactivated(windowEvent):

handleEvent("windowDeactivated", windowEvent) def windowIconified(windowEvent):

handleEvent("windowIconified", windowEvent) def windowDeiconified(windowEvent):

handleEvent("windowDeiconified", windowEvent) def handleEvent(event_name, event): print "Event: " + event_name print "Event info: " + "event.class" print "Source: " + "event.source.title" #print "Event Properties: " + "dir(event)" print "-----------------"

def createFrame(clazz, title): frame = clazz()

frame.windowClosing = windowClosing frame.windowOpened = windowOpened frame.windowClosed = windowClosed frame.windowIconified = windowIconified frame.windowDeiconified = windowDeiconified frame.WindowActivated = WindowActivated frame.windowDeactivated = windowDeactivated frame.size = 200, 200 frame.title = title frame.visible = 1 return frame def main():

raw_input ("Press Enter to create a JFC Frame") jfc_frame = createFrame(JFrame, "JFC Frame")

raw_input ("Press Enter to create an AWT Frame") it to work. awt_frame = createFrame(Frame, "AWT Frame")

I find myself using the Python way more often than the Java way, mainly because I have to import fewer classes and have to remember only the; name of the event handler, not every method in the listener.

Was this article helpful?

0 0

Post a comment