[Tkinter-discuss] tk_focusNext and tkfocusPrev

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Feb 14 08:41:21 CET 2008


Hunter0000 wrote:
> Hello,
> 
> Can anyone give an example of how to use these two methods? I've tried using
> them is various ways (the end goal being to replicate forward and reverse
> tabbing) but it always comes out saying either it doesn't exist or the
> object it returns in unsubscriptable. Any of the documentation I can find
> about most of the focus-related methods seems spotty at best, and in this
> case nonexistent, so an actual working example would be great to work from.
> 
> Mike

No idea myself, but this seems a decent explanation:-

http://docs.huihoo.com/tkinter/tkmanual/focusNext.html


Found with the power of Google :) - its a copy of the Tk man page in 
fact....


Now as to the real problem... tk_focusNext deal in windows (whole 
windows not single widgets) so... I think you want to use focus_get and 
focus_set like so:-

from Tkinter import *


root = Tk()
root.title("Focusing is HARD!!!")

e1 = Entry(root)
e1.focus_set()
e1.pack()

e2 = Entry(root)
e2.pack()

e3 = Entry(root)
e3.pack()

allE = [e1, e2, e3]
activeE = 0


def move_focus():
     global activeE
     activeE = activeE + 1
     allE[activeE].focus_set()

b = Button(root, command=move_focus)
b.pack()

root.mainloop()


### BUG fixing and Exception handling left to the reader!
### (IndexError left in as a nice surprise)
### the use of global is intentional even though I hate it

Cheers,
Martin.

-- 
signature file not found, must be something I ate


More information about the Tkinter-discuss mailing list