[Tutor] Bindings in Tkinter

Bryce Embry bryce@bembry.org
Wed, 29 May 2002 13:46:44 -0500


Howdy,
Not sure if this is the answer, but thought I'd suggest it at least.  In 
order for the keystroke bindings to work, the frame in question has to have 
the program focus.  When using a mouse button click, the button click moves 
the focus to the frame automatically.  For keystrokes to be bound, the 
widget has to have the focus before the button is pressed. Try binding the 
keystroke commands to the root / master widget and see if that works (i.e. 
self.master.bind('<Control-Shift-Up>', self.doStuff)).   There's also a way 
to make the frame take focus, but I've never tried it.  From the notes I 
have, it looks like you just set the option "takefocus = 1" to the frame 
widget, i.e. self.frame.top = Frame(master, takefocus = 1).  There are also 
some focus methods available that might help, but I'm not very familiar 
with them either.

Hope something in there helps.

Bryce

At 10:56 AM 5/29/2002, you wrote:
>Hello,
>
>I'm having difficulty with bindings, but I'm not real sure what I'm
>doing wrong.  The following is a small bit of code that is part of a
>larger app that I'm working on.  The idea is to simply bind the
><Control-Shift-Up> key to a little printing function, but it does
>nothing at all.
>
>The larger app actually contains things within the frames that are
>created, but I left them out of this for convenience and brevity.
>
>Any ideas?
>
>Sean Hendricks
>
>
>from Tkinter import *
>import Pmw
>
>class VTrans:
>         def __init__(self, master):
>                 self.master = master
>                 self.frame = Frame(master)
>                 self.frame.top = Frame(master)
>                 self.frame.bottom = Frame(master)
>                 self.frame.top.pack(side=TOP, fill=X)
>                 self.frame.bottom.pack(side=TOP)
>                 self.frame.pack()
>                 self.getBindings()
>
>         def doStuff(self, event=None):
>                 print "I am the very model of a ..."
>
>         def getBindings(self):
>                 self.frame.top.bind('<Control-Shift-Up>', self.doStuff)
>
>if __name__ == "__main__":
>         root = Tk()
>         root.title('Test')
>         Pmw.initialise()
>         main = VTrans(root)
>         root.mainloop()
>
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor


Bryce Embry
Geek-Of-All-Trades / Master-Of-None

www.bembry.org
--------------------------------------------------------------------------------------------------------------------------------------------------------
Technology Coordinator for MHA/FYOS ^ 390 South White Station ^ Memphis, TN 
38117 ^ (901)682-2409
--------------------------------------------------------------------------------------------------------------------------------------------------------