ANN: Thinking in Tkinter

Russell E. Owen owen at nospam.invalid
Thu Sep 12 13:29:48 EDT 2002


In article <b16e4ef7.0209120504.62d3c5bd at posting.google.com>,
 steve at ferg.org (Stephen Ferg) wrote:

>...
>This explains why "command" binding doesn't automatically pass an
>event object the way that event binding does.  The standard behavior
>of a widget  might be defined across multiple kinds of events  (e.g.
>Button's standard behavior is defined across both a button-press and a
>button-release), so there is no *single* event that could be passed.

True. It would help if the command callback sent an arguement that 
identified the widget (making it look a bit more like an event binding). 
In any event, it doesn't and command is unfortunately still the best way 
to handle button widgets.

In general Tkinter has several useful callback mechanisms, each of which 
is especially easy to use for a particular type of problem and may be a 
headache for other sorts of problems (e.g. events to simulate button 
command). Examples (aside from Button command):

Event binding examples:
- Use mouse button events to do useful things in the Canvas widget. For 
instance you can easily drag objects around by binding to <Button-1> and 
<B1-Motion>. Or show information when the pointer is near an object on a 
Canvas using <Motion>.
- Use keyboard events to modify the behavior of Entry widgets, for 
example bind to the return keyto accept the data and use it somehow, or 
bind to up-arrow and down-arrow to implement a history mechanism.

Variable trace:
- To implement input validation in Entry widgets, specify a textvar and 
put a trace on the variable. In your callback beep and restore the old 
value of the variable if the proposed new value is invalid.

There are others. My Tkinter Summary 
<http://www.astro.washington.edu/owen/TkinterSummary.html> section 
"Events and Callbacks" has what I believe is a complete set though 
without examples. I'm should take another look at it and expand it a bit.

-- Russell



More information about the Python-list mailing list