Tkinter bind single key

Neal Norwitz neal at metaslash.com
Tue Nov 6 11:47:28 EST 2001


John Hunter wrote:

Try using the "dialog".  In your example, this would be root. ie:

	root.bind('z', press_z)

I think I had to do this to get keys to work.

Neal
--
> I am working with the key 'z' to simplify.  All I want to do is bind
> an event to the 'z' keypress.  I have tried (without success):
> 
>    frame.bind('<z>', press_z)
>    frame.bind('<Key-z>', press_z)
>    frame.bind('<KeyPress-z>', press_z)
>    frame.bind('KeyPress-z', press_z)
>    frame.bind('Key-z', press_z)
>    frame.bind('z', press_z)
> 
> Ditto for 'plus'
> 
> My keysymdef entries are:
> 
>    #define XK_z                   0x07a
>    #define XK_plus                0x02b
> 
> Here is the test script; the mouse binding works fine:
> 
> #!/usr/local/bin/python
> 
> from Tkinter import *
> 
> def press_z(event):
>     print 'You pressed z'
> 
> def press_mouse(event):
>     print 'You pressed mouse-1'
> 
> root = Tk()
> frame = Frame(root, width=500,height=500)
> frame.bind('<z>', press_z)
> frame.bind('<Button-1>', press_mouse)
> frame.pack()
> root.mainloop()



More information about the Python-list mailing list