1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="../PythonCard.css" type="text/css" />
<title>Framework Overview - Events and Handlers</title>
</head>
<body>
<div id="banner">
<h1>Framework Overview - Events and Handlers</h1>
</div>
<?php include "../sidebar.php" ?>
<div id="content">
<p>Updated: April 10, 2004 for release 0.7.3</p>
<p>I'll cover the main points from a user point of view.</p>
<p>I listed most of the events supported by PythonCard components in an earlier message, but here they are again:</p>
<pre>
gainFocus
loseFocus
mouseContextDoubleClick
mouseContextDown
mouseContextUp
mouseDoubleClick
mouseDown
mouseDrag
mouseEnter
mouseLeave
mouseMiddleDoubleClick
mouseMiddleDown
mouseMiddleUp
mouseMove
mouseUp
timer
</pre>
<p>The most common events you see in the samples are not the ones above, but these:</p>
<pre>
command
mouseClick
openBackground
select
</pre>
<p>Every component and menu can have an associated command event.
As an example, here's a menu item definition from addresses.rsrc.py that uses 'command' and the associated handler.</p>
<pre>
{ 'type':"MenuItem",
'name':"menuEditNewCard",
'label':"&New Card\tCtrl+N",
'command':'editNewCard'},
def on_editNewCard_command(self, event):
self.newRecord()
</pre>
<p>All component and menu event handlers take the form
<pre> on_commandName_command</pre>
or
<pre> on_componentName_eventName</pre>
more examples...
<pre>
def on_menuFileExit_select(self, event):
def on_btnColor_mouseClick(self, event):
def on_bufOff_mouseDrag(self, event):
</pre>
<p>The underscores are used by the framework to identify a handler method and
they have the added benefit of making the handlers stick out from other
methods in the user code. All events go first to the target of the event and
if they aren't handled, then the framework searches for a match at the
background level. The tictactoe sample shows an example of using a background
handler for mouseClick events.</p>
<p>It is common to have an openBackground event such as </p>
<pre>
def on_openBackground(self, event):
</pre>
<p>which can be used to perform app initialization (in the prototype framework
it is more like the HyperCard openStack system message). The other background events are:</p>
<pre>
activate
deactivate <- will be added in release 0.8
close
idle <- sent when the message queue empties
minimize
maximize
move
size
</pre>
<p>The handler method arguments (self, event) are just names, so like
any other Python program you can use what names you want, but for
readability it is better to use self and event. The 'target' of the
event can be referenced as event.target, which simplifies manipulating
the component. In the example below bufOff is a BitmapCanvas and line is one
of its methods.</p>
<pre>
def on_bufOff_mouseDrag(self, event):
x, y = event.position
event.target.line(self.x, self.y, x, y)
self.x = x
self.y = y
</pre>
<hr>
| <a href="general_concepts_and_limitations.html">General Concepts and Limitations</a>
| <a href="components.html">Components</a>
| <a href="dialogs.html">Dialogs</a>
| <a href="events_and_handlers.html">Events and Handlers</a>
| <a href="menus.html">Menus</a>
| <a href="resource_files.html">Resource Files</a>
| <a href="runtime_tools.html">Runtime Tools</a>
<hr>
<?php include "../footer.php" ?>
<p>$Revision: 1.2 $ : $Author: kasplat $ : Last updated $Date: 2004/08/14 21:05:15 $</p>
</div> <!-- end of content -->
</body>
</html>
|