tabbing through Tk Text widgets

Eric Brunel eric_brunel at despammed.com
Mon Jul 19 03:58:41 EDT 2004


Elaine Jackson wrote:
> I've got a Tk window with a large number of Text widgets, and I'd like to have
> the TAB key move focus from one Text widget to the next. Unfortunately I can't
> find the recipe. Can anybody help?
> 
> Peace

See methods tk_focusNext & tk_focusPrev in 
http://www.pythonware.com/library/tkinter/introduction/x9374-event-processing.htm

Here is an example:

----------------------------------------------------
from Tkinter import *

root = Tk()

t1 = Text(root)
t1.pack(side=TOP)

t2 = Text(root)
t2.pack(side=TOP)

def focusNext(widget):
   widget.tk_focusNext().focus_set()
   return 'break'

def focusPrev(widget):
   widget.tk_focusPrev().focus_set()
   return 'break'

for t in (t1, t2):
   t.bind('<Tab>', lambda e, t=t: focusNext(t))
   t.bind('<Shift-Tab>', lambda e, t=t: focusPrev(t))

t1.focus_set()

root.mainloop()
----------------------------------------------------

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




More information about the Python-list mailing list