Tkinter binding question

Frederic Rentsch anthra.norell at bluewin.ch
Tue Jun 19 11:55:48 EDT 2012


Rick, 

Thank you for your thorough discussion. I tried your little program.
Enter and leave work as expected. Pushing mouse buttons call
leave-enter, exactly as it happened with my code. So that seems to be a
default behavior. No big deal. Without the tracing messages it would go
unnoticed. Releasing either of the bound mouse buttons displays the
corresponding messages. So all works fine. 
   If I copy your event descriptors into my program, the button-release
callback still fails. It works in your code, not in mine. Here is what
my code now looks like. It is somewhat more complicated than yours,
because I bind Frames holding each a line (line_frame) and each frame
contains a few Labels side by side. The idea is to achieve a table with
vertically aligning columns each line of which I can click-select. (Is
there a better way?)

   for line_frame in ...:
      line_frame.bind ('<Enter>', self.color_selected)
      line_frame.bind ('<Leave>', self.color_selectable)
      line_frame.bind ('<ButtonRelease-1>', self.pick_record)
      line_frame.bind ('<ButtonRelease-3>', self.info_profile)
      line_frame.grid (row = n+1, column = 0)
      for i in self.range_n_fields:
         field = Label (line_frame, width = ..., text = ...    
         field.grid (row = 0, column = i, sticky = W)
      ...

   def color_selected (self, event):
      print 'hit list.color_selected ()'

   def color_selectable (self, event):
      print 'hit list.color_selectable ()'

   def pick_record (self, event):     # Nver gets called
      print 'hit list.pick_record ()'

   def info_profile (self, event):    # Never gets called
      print 'hit list.info_profile ()'

I admit that I don't have an accurate conception of the inner workings.
It's something the traveler on the learning curve has to acquire a feel
for by trial and error. In this case the differing behavior should
logically have to do with the structural difference: I bind Labels that
contain Labels. If I click this nested assembly, who gets the event? The
contained widget, the containing widget or both?

Frederic


Incidentally, my source of inspiration for chaining event descriptors
was the New Mexico Tech Tkinter 8.4 reference, which says: ... In
general, an event sequence is a string containing one or more event
patterns. Each event pattern describes one thing that can happen. If
there is more than one event pattern in a sequence, the handler will be
called only when all the patterns happen in that same sequence ...

Again, predicting the precedence with overlaps is much like solving a
murder case: finding suspects and let the innocent ones off the hook.
The only reference I have found on that topic is in effbot.org's
tkinterbook, which says that precedence goes to the "closest match", but
doesn't explain how one evaluates "closeness".





More information about the Python-list mailing list