Creating the Event Class
An event class defines the custom event. Of course, you have to also write code to implement and
respond to the custom event, but let’s focus on the definition first. The most basic event class must
include four activities:
➤
An initialization that defines a container for holding a list of event handlers
➤
A method for adding new event handlers
➤
➤
A method for removing old event handlers
➤
➤
➤
➤
A method that calls the event handlers in turn whenever an external source invokes (fires)
the event
You can always add more items to your event handler, but a basic event handler must include these
four items. With this in mind, Listing 8-5 shows a basic event definition. You might be surprised at
how little code you need to perform this task.
FIguRE 8-8:
The Timer component updates
the time shown in this example.
548592c08.indd 153
2/24/10 12:48:11 PM
www.finebook.ir
154
❘
CHAPTER 8
Creating WindoWs Forms appliCations
lISTINg 8-5:
Defining a simple event class
class MyEvent:
# Create the initial HandlerList.
def __init__(self):
self.HandlerList = set()
# Add new handlers to the list.
def Add(self, NewHandler):
self.HandlerList.add(NewHandler)
# Remove existing handlers from the list.
def Remove(self, OldHandler):
try:
self.HandlerList.remove(OldHandler)
except KeyError:
pass
# Invoke the handler.
def Fire(self, Msg):
# Call each of the handlers in the list.
for self.Handler in self.HandlerList:
self.Handler(Msg)
The code begins with
__init__()
, which IronPython calls automatically anytime someone creates
an event of this type. The only purpose of
__init__()
is to create a container for storing event
handler references. You can use any container you want, but the example relies on a
set()
because
this particular container works very well as a means of storing event handlers. The initialization
code creates an empty
set()
that you’ll later fill with event handler references.
There are many different ways to create a delegate using IronPython — this
chapter shows one of the more basic techniques you can use. However, you
might find that this technique doesn’t work for your particular need. It’s
always a good idea to look at what other people are doing with Python and
IronPython. For example, there’s another example of an event system at
http://www.valuedlessons.com/2008/04/events-in-python.html
. In this
case, the author wanted to create a lightweight event system that mimicked
C#. Another, more ambitious example is at
http://code.activestate.com/
recipes/410686/
. Don’t forget the
pyevent.py
class provided with the
IronPython tutorial (it does work). The point is that you don’t want to give
up on events in IronPython. If what you want doesn’t exist now, you can
probably create it without too much trouble.
The
Add()
method simply adds a reference to the event handler passed as one of the arguments. In
this case, the code uses the
self.HandlerList.add()
method to perform the task.
548592c08.indd 154
2/24/10 12:48:11 PM
www.finebook.ir
Do'stlaringiz bilan baham: |