Wednesday, 16 January 2013

Python Language Lesson: callback function


Python Language Lesson: callback function

A callback function is an ordinary function, defined similarly to any
other function, but it is used for a specific purpose. There is no technical
difference between ordinary functions and callback functions. The
distinction is made to clarify discussion.
Typically, a callback function is called by a function in the PyS60
library to respond to a specific event – such as when the user has
chosen a menu item or decides to quit the application. In contrast,
ordinary functions are called in your application code to handle
application-specific tasks. However, in some cases a callback function
may be called by your application explicitly.
Associating a function with an event is often called binding. Some
PyS60 objects, such as Canvas and Inbox, include a function called
bind() that is used to bind a callback function to some event related
to the object.Whenever the PyS60 API documentation or this book asks you to
provide a callback function, do not add parentheses after the function
name since the function is only called after the event occurs. If you
are familiar with C or C++, you might notice that this is similar to how
function pointers are passed around in these languages.



In Example 11, we then assign the application name to the title variable
appuifw.app.title. This string shows at the top of the screen when
the application is running.
As we discussed at the beginning of this chapter, the PyS60 examples
we have seen so far are executed line by line and they exit when the last
line has been executed. However, Example 11 should not exit until the
user decides to do so. Therefore, our application should not drop out after
the last line has been executed, but instead should wait for user action.
This is accomplished with an object called Ao lock that is part of
module e32. The object includes a function called wait() that puts the
application into a waiting mode until the lock is explicitly released with the
signal() function. The signal() function must be called when you
want to terminate the application, so we call it inside the quit function.
To see why it’s important to have the lock at the end of the application
code, omit the line, app lock.wait(), from your code and run the
application. A dialog pops up but the application does not go into a wait
state; it finishes instantly.
When you run Example 11, you should see the ‘First App!’ title at the
top of the screen (as in Figure 4.2) and a dialog popping up. Nothing else
happens until you press the Exit key (the right softkey), which shuts down
the application. After this, you should see the text ‘Exit key pressed!’ and
‘Application exits’ messages in the PyS60 console on the phone screen.
It might be difficult to notice that the application is running at all. This
is because the PyS60 interpreter is built using exactly the same application
framework, so it looks the same as any other PyS60 application.









No comments:

Post a Comment