[Tutor] Changing Keyboard output

Michael Lange klappnase at freenet.de
Sun Mar 20 21:18:16 CET 2005


On Sun, 20 Mar 2005 19:38:40 -0000
"Igor Riabtchuk" <igor.r at vodafone.net> wrote:

> Hi,
> 
> I am totally new to Python and still learning.
> 
> I am looking for a way to change keyboard output within Tkinter widget - for example, say I press "p" and I want it to come out as "t".
> 
> Could anyone possibly point me in the right direction?
> 
> Igor 

You can use the widget's bind() method to replace the standard callback with a new one:

from Tkinter import *
root = Tk()
e = Entry(r)
e.pack()

def PtoT(event):
    e.insert('insert', 't')
    return 'break'

e.bind('<p>', PtoT)

the "return 'break'" statement prevents the event from being propagated to Tk's standard event handler;
without it both "p" and "t" would be inserted into the Entry.

I hope this helps

Michael


More information about the Tutor mailing list