12.07.2015 Views

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

Tkinter reference: A GUI for Python

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!