Setting focus to specific TKinter entry widget
Asked Answered
Y

2

28

I'd like to set the focus of my program to a specific entry widget so that I can start entering data straight-away - how can I do this?

My current code

from Tkinter import *
root = Tk()
frame=Frame(root,width=100,heigh=100,bd=11)
frame.pack()
label = Label(frame,text="Enter a digit that you guessed:").pack()
entry= Entry(frame,bd=4)
entry.pack()

button1=Button(root,width=4,height=1,text='ok')
button1.pack()

root.mainloop()
Yukikoyukio answered 29/11, 2012 at 13:0 Comment(0)
G
48

Use entry.focus():

from Tkinter import *
root = Tk()
frame=Frame(root,width=100,heigh=100,bd=11)
frame.pack()
label = Label(frame,text="Enter a digit that you guessed:").pack()
entry= Entry(frame,bd=4)
entry.pack()
entry.focus()
button1=Button(root,width=4,height=1,text='ok')
button1.pack()

root.mainloop()
Gastrotomy answered 29/11, 2012 at 13:11 Comment(1)
I have the entry prefilled with a variable. The cursor is nevertheless at the first position and you have to got the right to type in or alter the last characters. Is there a way to automatically jump at the end?Stray
C
6

I tried this way and it is ok

entry= Entry(root)
entry.focus()
entry.pack
Custodial answered 16/12, 2019 at 10:30 Comment(1)
Missing parentheses: entry.pack()Lionel

© 2022 - 2024 — McMap. All rights reserved.