Tkinter: Distinguishing different

Tim Daneliuk tundra at tundraware.com
Sat Dec 28 18:30:07 EST 2002


Jeff Epler wrote:
> On Sat, Dec 28, 2002 at 12:40:07AM +0000, Tim Daneliuk wrote:
> 
>>I have an existing program that binds a feature to:
>>
>>        <Control-Double-ButtonRelease3>
>>
>>I now want to add a new feature which binds to:
>>
>>        <ButtonRelease-3>
>>
>>The problem is that the handler for the second is always called even
>>when I am pressing the key combination for the first. Obviously, the
>>"ButtonRelease-2" is checked by the message dispatcher before the
>>Control ... combination is checked.
> 
> 
> What you describe is in direct conflict with Tk's documentation, or else
> you're not describing it clearly enough.  Assuming both bindings are made
> on the same widget or tag, the more specific binding will be the one used.
> So if you have a binding for <X> and <Control-X>, the first binding will
> fire for the event <X>, and the event <Alt-X>, and the second binding
> will fire for <Control-X> and <Alt-Control-X>.

It is entirely likely this is a misunderstanding on my part, and I
probably did not explain myself well but ...

Run the code attached below.  When I press and release Button-3 it
should print "One" - it does this just fine.  BUT, when I press
Control-Double-ButtonRelease-3, I get BOTH "One" and "Two" printed,
when all I wanted was the latter.  IOW, Tk is firing both handlers.
This is what I want to avoid somehow...

Also, within an event handler, is there a generic way to determine
if a particular modifying key has been pressed?  I want to be able to
distinquish Alt-x from x within a handler rather than by binding them
separately.


#---------------------- Test Program Follows -----------------#
#!/usr/bin/env python
from Tkinter import *

MOUSEONE   = '<ButtonRelease-3>'
MOUSETWO   = '<Control-Double-ButtonRelease-3>'

class myUI:

     def __init__(self, root):

         self.ListBox = Listbox(root,
                                selectmode=EXTENDED,
                                exportselection=0,
                                )
         self.ListBox.pack(side=LEFT, fill=BOTH, expand=1)

         # End if method 'twanderUI.__init__()'

         self.ListBox.bind(MOUSEONE, one)
         self.ListBox.bind(MOUSETWO, two)


# End of class definition, 'twanderUI'

def one(event):
     print "One"

def two(event):
     print "Two"

UIroot = Tk()
UI = myUI(UIroot)
UIroot.tkraise()
UIroot.mainloop()


------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com




More information about the Python-list mailing list