[Tkinter-discuss] Switching between Entry widgets

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Jul 21 11:53:40 CEST 2005


Jay Taneja wrote:
> hi everyone - i'm relatively new to python/tkinter, so it may be
> something pretty silly i've overlooked.
> 
> the tkinter application i have written has two entry widgets and two
> buttons. what i would like is for the focus to be on the first entry
> widget, such that i launch the program and can immediately input text
> into the entry box. for some reason, i am unable to get the focus()
> command working for me.
> 
> additionally, the input device i am using to put text in the entry
> boxes ends in a carriage return. as soon as this carriage return is
> received in the first entry box, i would like the focus to go to the
> second entry box. when the second entry box gets a carriage return, i
> would like to issue the callback of the first button widget.
> 
> this all seems to make sense in my head, binding the <Return> keypress
> to switch the focus, but i can't seem to get everything working
> correctly. the program doesn't respond to my bindings and i can't get
> the focus switching to work at all.
> 
> any help would be greatly appreciated. thank you.
> 
> -jay


Jay,


The method you are looking for is focus_set, also this will be used to
change the focus when you press return somthing like this:-


e1 = Entry(root)
e1.pack()

e2 = Entry(root)
e2.pack()

def return_bind(event):
     """called when <Return> event in e1

     will give the focus to e2
     """
     e2.focus_set()

e1.bind("<Return>", return_bind)


# give the focus to e1 at the start
e1.focus_set()





Martin













More information about the Tkinter-discuss mailing list