22
votes

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()
2

2 Answers

37
votes

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()
1
votes

I tried this way and it is ok

entry= Entry(root)
entry.focus()
entry.pack