Tkinter: text widget predefined key bindings

Eric Brunel eric.brunel at pragmadev.com
Tue Aug 27 19:05:44 CEST 2002


Michele Simionato wrote:
> - I discovered by trials that the text widget recognizes some
> predefined key bindings; for instance C-k delete a line, C-o insert
> a newline,C-e goes at the end of a line, C-t transpose two chars, C-i
> insert a tab, etc. etc. These settings sometimes are useful, sometimes
> are annoying. Suppose for instance I want to rebind, C-k:
> 
> text.bind('<Control-k>',lambda event : dosomething())
> 
> it works, in the sense that  dosomething() is executed, but
> unfortunately the line is killed too !
> 
> text.unbind('<Control-k>') doesn't work.
> 
> How can I get rid of these hard coded bindings ?

The bindings are not done at the widget level, but at the class level. Try:

text.bind_class('Text', '<Control-k>', lambda e: None)
text.bind('<Control-k>', lambda event : dosomething())

and it should work as you expect.

> - Second question: is there some predefined binding for undo ?
> C-z and C-u do not work. If there is no undo, how could I implement
> some simple undo routine ?

AFAIK, you can't... And implementing an undo function is always quite a 
PITA, because sooooooooo many things can happen to your text that 
Tk/Tkinter won't care to warn you about... I heard some Tk extension 
package (Blt?) had a Text widget with a built-in undo function, but I never 
used it.

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list