1 line
3.2 KiB
HTML
1 line
3.2 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<title>PyGUI - Actions</title>
|
|
|
|
<meta http-equiv="content-type"
|
|
content="text/html; charset=ISO-8859-1">
|
|
</head>
|
|
<body>
|
|
|
|
<h1>Actions</h1>
|
|
Some PyGUI classes have one or more <i>action properties</i>. An action
|
|
property specifies an action to perform in response to something done by
|
|
the user, e.g. clicking a button.<br>
|
|
<br>
|
|
The value of an action property can take any of the following forms:<br>
|
|
|
|
<ul>
|
|
<li>A <i>function</i> (or other callable object)</li>
|
|
<li>A tuple <tt>(</tt><i>function</i><tt>,</tt> <i>arg</i><tt>,</tt> ...<tt>)</tt></li>
|
|
<li>A <i>message name</i> specifying a message to be sent up the message
|
|
handling hierarchy</li>
|
|
<li>A tuple <tt>(</tt><i>message</i><tt>,</tt> <i>arg</i><tt>,</tt> ...<tt>)</tt><br>
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<h2>Invoking actions programmatically</h2>
|
|
As a general principle, action properties are only triggered in response
|
|
to actions performed by the user, not actions performed by the program. For
|
|
example, the <tt>action</tt> property of a CheckBox is triggered when the
|
|
user clicks on the check box, but not when the <tt>on</tt> property of the
|
|
check box is assigned to by program code. This makes it easier to set up
|
|
actions which keep different controls in sync with each other without inadvertently
|
|
creating infinite loops.<br>
|
|
<br>
|
|
If you do want an action performed in response to program code, you will
|
|
need to trigger it explicitly. Corresponding to each action property <tt>xxx_action</tt>
|
|
there is a method called <tt>do_xxx_action</tt> which invokes the action.
|
|
For example, here is how you can change the <tt>on</tt> property of a check
|
|
box and trigger the action as though the user had clicked on the control:<br>
|
|
<blockquote><tt>my_checkbox.on = True</tt><br>
|
|
<tt>my_checkbox.do_action()</tt><br>
|
|
</blockquote>
|
|
<h2>Overriding actions in subclasses</h2>
|
|
If you subclass a component and want the subclass to provide an action for
|
|
one of the superclass's action properties, there are a couple of ways you
|
|
can go about it. One is to simply pass a bound method of the subclass as
|
|
the initial value of the action property when calling the superclass's __init__
|
|
method. This is not the best way, however, since it uses up the action slot
|
|
and precludes the possibility of users of your class using it to further
|
|
customise the component's behaviour. Worse, if the user doesn't realise the
|
|
action slot is already in use and plugs in an action of his own, it will
|
|
supplant your subclass's action, which is probably not what you want.<br>
|
|
<br>
|
|
A better way is to override the <tt>do_action</tt> method for the action.
|
|
This ensures that users of your class can't inadvertently wipe out your action,
|
|
and leaves the action property open for further customisation, which you
|
|
can allow by calling the superclass's <tt>do_action</tt> method from yours.
|
|
For example,<br>
|
|
<blockquote><tt>class MyControl(SomeControl):</tt><br>
|
|
<br>
|
|
<tt> def do_action(self):</tt><br>
|
|
<tt> ...do something special here,
|
|
and then...</tt><br>
|
|
<tt> SomeControl.do_action(self) #
|
|
give user's action a go</tt><br>
|
|
</blockquote>
|
|
<br>
|
|
---<br>
|
|
<br>
|
|
</body>
|
|
</html>
|