Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter set focus on Entry widget

Here is my code:

import tkinter as tk
userData = tk.Tk()
nbdays = tk.IntVar()
mainframe = tk.Frame(userData, relief= 'raised', borderwidth=1)
tk.Label(mainframe, text = 'Number of days', font = 10).place(x=2, y = 30)
tk.Entry(mainframe, width= 8, textvariable = nbdays).place(x= 200, y= 30)

[....]

How do I set the focus on that last tk.Entry(.) widget?

like image 811
Cyrus TNZ63 Avatar asked Mar 04 '14 02:03

Cyrus TNZ63


People also ask

What does focus () do in Tkinter?

Focus is used to refer to the widget or window which is currently accepting input. Widgets can be used to restrict the use of mouse movement, grab focus, and keystrokes out of the bounds.

What is the purpose of the focus method in a Tkinter widget Python?

It focuses the widget and makes them active until the termination of the program.

What does Focus_set () do in Python?

focus_set() method- This method is used to set the focus on the desired widget if and only if the master window is focused.

What is Textvariable in entry Tkinter?

PythonTkinter entry textvariable textvariable is used to provide value through a variable. value can be Integer or String. for integer : IntVar() keyword is used. for String: StringVar() keyword is used.


1 Answers

Use the focus_set method which is common to all widgets:

entry = tk.Entry(...)
entry.place(...)
entry.focus_set()
like image 174
Bryan Oakley Avatar answered Oct 01 '22 11:10

Bryan Oakley