Many widgets are provided with an outline called the focus highlight that shows theuser which widget has the highlight. This is normally a thin black frame locatedjust outside the widget’s border (if any). For widgets that don’t normally have afocus highlight (specifically, frames, labels, and menus), you can set its highlightthicknessoption to a nonzero value to make the focus highlight visible. You can also change the color of the focus highlight using the highlightcoloroption. Widgets of class Frame, Label, and Menu are not normally visited by the focus.However, you can set their takefocus options to 1 to get them included in focustraversal. You can also take any widget out of focus traversal by setting itstakefocus option to 0.The order in which the TAB key traverses the widgets is: For widgets that are children of the same parent, focus goes in the same order thewidgets were created. For parent widgets that contain other widgets (such as frames), focus visits theparent widget first (unless its takefocus option is 0), then it visits the child widgets,recursively, in the order they were created.To sum up: to set up the focus traversal order of your widgets, create them in that order.Remove widgets from the traversal order by setting their takefocus options to 0, and<strong>for</strong> those whose default takefocus option is 0, set it to 1 if you want to add them to theorder.The above describes the default functioning of input focus in <strong>Tkinter</strong>. There is another,completely different way to handle it—let the focus go wherever the mouse goes. UnderUniversal widget methods, above, refer to the .tk_focusFollowsMouse() method.You can also add, change or delete the way any key on the keyboard functions inside anywidget by using event bindings. See Events, below, <strong>for</strong> the details.22. Events: responding to stimuliAn event is something that happens to your application—<strong>for</strong> example, the user presses akey or clicks or drags the mouse—to which the application needs to react.The widgets normally have a lot of built-in behaviors. For example, a button will reactto a mouse click by calling its command callback. For another example, if you move thefocus to an entry widget and press a letter, that letter gets added to the content of thewidget.However, the event binding capability of <strong>Tkinter</strong> allows you to add, change, or deletebehaviors.First, some definitions: An event is some occurrence that your application needs to know about. An event handler is a function in your application that gets called when an eventoccurs. We call it binding when your application sets up an event handler that gets calledwhen an event happens to a widget.New Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: Events Page 76
22.1 Levels of bindingYou can bind a handler to an event at any of three levels:1. Instance binding: You can bind an event to one specific widget. For example, youmight bind the PAGEUP key in a canvas widget to a handler that makes the canvasscroll up one page. To bind an event of a widget, call the .bind() method on thatwidget (see Universal methods, above).For example, suppose you have a canvas widget named self.canv and you wantto draw an orange blob on the canvas whenever the user clicks the mouse button 2(the middle button). To implement this behavior:self.canv.bind ( "", self.__drawOrangeBlob )The first argument is a sequence descriptor that tells <strong>Tkinter</strong> that wheneverthe middle mouse button goes down, it is to call the event handler namedself.__drawOrangeBlob. (See the section Writing an event handler, below, <strong>for</strong>an overview of how to write handlers such as .__drawOrangeBlob()). Note thatyou omit the parentheses after the handler name, so that <strong>Python</strong> will pass in a<strong>reference</strong> the handler instead of trying to call it right away.2. Class binding: You can bind an event to all widgets of a class. For example, youmight set up all Button widgets to respond to middle mouse button clicks bychanging back and <strong>for</strong>th between English and Japanese labels. To bind an event toall widgets of a class, call the .bind_class() method on any widget (see Universalmethods, above).For example, suppose you have several canvases, and you want to set up mousebutton 2 to draw an orange blob in any of them. Rather than having to call .bind()<strong>for</strong> every one of them, you can set them all up with one call something like:self.bind_class ( "Canvas", "",self.__drawOrangeBlob )3. Application binding: You can set up a binding so that a certain event calls a handlerno matter what widget has the focus or is under the mouse. For example, you mightbind the PRINTSCRN key to all the widgets of an application, so that it prints thescreen no matter what widget gets that key. To bind an event at the applicationlevel, call the .bind_all() method on any widget (see Universal methods, above).Here’s how you might bind the sc PrintScrn key, whose “key name” is "Print":self.bind_all ( "", self.__printScreen )New Mexico Tech Computer Center <strong>Tkinter</strong> <strong>reference</strong>: Events Page 77