| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node730.html" /> |
| 13 | <link rel="prev" href="node728.html" /> |
| 14 | <link rel="parent" href="node722.html" /> |
| 15 | <link rel="next" href="node730.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>16.1.6.7 Bindings and Events</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="16.1.6.6 Tk Option Data" |
| 25 | href="node728.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="16.1.6 Handy Reference" |
| 28 | href="node722.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="16.1.6.8 The index Parameter" |
| 31 | href="node730.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="node728.html">16.1.6.6 Tk Option Data</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="node722.html">16.1.6 Handy Reference</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node730.html">16.1.6.8 The index Parameter</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION0018167000000000000000"> |
| 56 | 16.1.6.7 Bindings and Events</A> |
| 57 | </H3> |
| 58 | <P> |
| 59 | <a id='l2h-4840' xml:id='l2h-4840'></a> |
| 60 | |
| 61 | <P> |
| 62 | The bind method from the widget command allows you to watch for |
| 63 | certain events and to have a callback function trigger when that event |
| 64 | type occurs. The form of the bind method is: |
| 65 | |
| 66 | <P> |
| 67 | <div class="verbatim"><pre> |
| 68 | def bind(self, sequence, func, add=''): |
| 69 | </pre></div> |
| 70 | where: |
| 71 | |
| 72 | <P> |
| 73 | <DL> |
| 74 | <DT><STRONG>sequence</STRONG></DT> |
| 75 | <DD>is a string that denotes the target kind of event. (See the bind |
| 76 | man page and page 201 of John Ousterhout's book for details). |
| 77 | |
| 78 | <P> |
| 79 | </DD> |
| 80 | <DT><STRONG>func</STRONG></DT> |
| 81 | <DD>is a Python function, taking one argument, to be invoked when the |
| 82 | event occurs. An Event instance will be passed as the argument. |
| 83 | (Functions deployed this way are commonly known as <var>callbacks</var>.) |
| 84 | |
| 85 | <P> |
| 86 | </DD> |
| 87 | <DT><STRONG>add</STRONG></DT> |
| 88 | <DD>is optional, either "<tt class="samp"></tt>" or "<tt class="samp">+</tt>". Passing an empty string |
| 89 | denotes that this binding is to replace any other bindings that this |
| 90 | event is associated with. Preceeding with a "<tt class="samp">+</tt>" means that this |
| 91 | function is to be added to the list of functions bound to this event type. |
| 92 | </DD> |
| 93 | </DL> |
| 94 | |
| 95 | <P> |
| 96 | For example: |
| 97 | <div class="verbatim"><pre> |
| 98 | def turnRed(self, event): |
| 99 | event.widget["activeforeground"] = "red" |
| 100 | |
| 101 | self.button.bind("<Enter>", self.turnRed) |
| 102 | </pre></div> |
| 103 | |
| 104 | <P> |
| 105 | Notice how the widget field of the event is being accessed in the |
| 106 | <tt class="method">turnRed()</tt> callback. This field contains the widget that |
| 107 | caught the X event. The following table lists the other event fields |
| 108 | you can access, and how they are denoted in Tk, which can be useful |
| 109 | when referring to the Tk man pages. |
| 110 | |
| 111 | <P> |
| 112 | <div class="verbatim"><pre> |
| 113 | Tk Tkinter Event Field Tk Tkinter Event Field |
| 114 | -- ------------------- -- ------------------- |
| 115 | %f focus %A char |
| 116 | %h height %E send_event |
| 117 | %k keycode %K keysym |
| 118 | %s state %N keysym_num |
| 119 | %t time %T type |
| 120 | %w width %W widget |
| 121 | %x x %X x_root |
| 122 | %y y %Y y_root |
| 123 | </pre></div> |
| 124 | |
| 125 | <P> |
| 126 | |
| 127 | <DIV CLASS="navigation"> |
| 128 | <div class='online-navigation'> |
| 129 | <p></p><hr /> |
| 130 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 131 | <tr> |
| 132 | <td class='online-navigation'><a rel="prev" title="16.1.6.6 Tk Option Data" |
| 133 | href="node728.html"><img src='../icons/previous.png' |
| 134 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 135 | <td class='online-navigation'><a rel="parent" title="16.1.6 Handy Reference" |
| 136 | href="node722.html"><img src='../icons/up.png' |
| 137 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 138 | <td class='online-navigation'><a rel="next" title="16.1.6.8 The index Parameter" |
| 139 | href="node730.html"><img src='../icons/next.png' |
| 140 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 141 | <td align="center" width="100%">Python Library Reference</td> |
| 142 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 143 | href="contents.html"><img src='../icons/contents.png' |
| 144 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 145 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 146 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 147 | <td class='online-navigation'><a rel="index" title="Index" |
| 148 | href="genindex.html"><img src='../icons/index.png' |
| 149 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 150 | </tr></table> |
| 151 | <div class='online-navigation'> |
| 152 | <b class="navlabel">Previous:</b> |
| 153 | <a class="sectref" rel="prev" href="node728.html">16.1.6.6 Tk Option Data</A> |
| 154 | <b class="navlabel">Up:</b> |
| 155 | <a class="sectref" rel="parent" href="node722.html">16.1.6 Handy Reference</A> |
| 156 | <b class="navlabel">Next:</b> |
| 157 | <a class="sectref" rel="next" href="node730.html">16.1.6.8 The index Parameter</A> |
| 158 | </div> |
| 159 | </div> |
| 160 | <hr /> |
| 161 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 162 | </DIV> |
| 163 | <!--End of Navigation Panel--> |
| 164 | <ADDRESS> |
| 165 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 166 | </ADDRESS> |
| 167 | </BODY> |
| 168 | </HTML> |