ANN: Thinking in Tkinter

jepler at unpythonic.net jepler at unpythonic.net
Wed Sep 11 22:09:44 EDT 2002


(whoops, I had intended to send this to python-list)

On Wed, Sep 11, 2002 at 10:05:53AM -0500, Jeff Epler wrote:
> You'd have to read the tcl source for the button bindings to see what's
> going on.  I'll walk you through them ...
> 
> 
> bind Button <1> {
>     tkButtonDown %W
> }
> bind Button <ButtonRelease-1> {
>     tkButtonUp %W
> }
> bind Button <Enter> {
>     tkButtonEnter %W
> }
> bind Button <Leave> {
>     tkButtonLeave %W
> }
> 
> proc tkButtonDown w {
>     global tkPriv
>     set tkPriv(relief) [$w cget -relief]
>     if {[string compare [$w cget -state] "disabled"]} {
>         set tkPriv(buttonWindow) $w
>         $w configure -relief sunken -state active
>     }
> }
> 
> proc tkButtonUp w {
>     global tkPriv
>     if {[string equal $tkPriv(buttonWindow) $w]} {
>         set tkPriv(buttonWindow) ""
>         $w configure -relief $tkPriv(relief)
>         if {[string equal $tkPriv(window) $w]
>               && [string compare [$w cget -state] "disabled"]} {
>             $w configure -state normal
>             uplevel #0 [list $w invoke]
>         }
>     }
> }
> 
> proc tkButtonEnter w {
>     global tkPriv
>     if {[string compare [$w cget -state] "disabled"] \
>             && [string equal $tkPriv(buttonWindow) $w]} {
>         $w configure -state active -relief sunken
>     }
>     set tkPriv(window) $w
> }
> 
> proc tkButtonLeave w {
>     global tkPriv
>     if {[string compare [$w cget -state] "disabled"]} {
>         $w configure -state normal
>     }
>     if {[string equal $tkPriv(buttonWindow) $w]} {
>         $w configure -relief $tkPriv(relief)
>     }
>     set tkPriv(window) ""
> }
> 
> When the pointer leaves or enters the button, tkPriv(window) is
> set to that widget.  When a mouse button is pressed in a button,
> tkPriv(buttonWindow) is set.  When the mouse button is released, and
> window and buttonWindow both match the widget where the release took
> place, the button's -command is invoked.
> 
> These routines are actually slightly different for each of unix, mac,
> and Microsoft Windows in my copy of tk8.3.
> 
> In my opinion, it's great to understand this, and maybe to reimplement
> it in Python as an exercise, but since the vast majority of any GUI 
> application should use the standard behavior for its components as
> defined by the operating system/desktop environment, it makes much
> more sense to me to start teaching Tk with basics like -command, -text,
> -textvariable, the grid manager, and (in Python) things like Toplevel
> subclasses with standard buttons and the like.
> 
> Later, I might have a unit that "teaches" a compound widget like the
> "shufflebox", which does require custom bindings such as doubleclick to
> move selected items to the opposite box.
> 
> Jeff




More information about the Python-list mailing list