What is the difference between focus and focus_set methods in Tkinter?

In Tkinter, focus refers to which widget is currently accepting keyboard input. The focus() and focus_set() methods are used to programmatically set focus to a widget, making it active for user input.

Key Difference

Both focus() and focus_set() perform the same function - they are identical methods. The focus_set() method is simply an alias for focus(), provided for clarity and consistency with other Tkinter naming conventions.

Syntax

widget.focus()
# OR
widget.focus_set()

Example

Here's how to use both methods to set focus on an Entry widget ?

import tkinter as tk
from tkinter import ttk

# Create the main window
root = tk.Tk()
root.geometry("400x200")
root.title("Focus Example")

# Create Entry widgets
entry1 = tk.Entry(root, width=20)
entry1.pack(pady=10)

entry2 = tk.Entry(root, width=20)
entry2.pack(pady=10)

# Functions to set focus using different methods
def focus_first():
    entry1.focus()  # Using focus()

def focus_second():
    entry2.focus_set()  # Using focus_set()

# Create buttons
ttk.Button(root, text="Focus Entry 1 (focus)", command=focus_first).pack(pady=5)
ttk.Button(root, text="Focus Entry 2 (focus_set)", command=focus_second).pack(pady=5)

root.mainloop()

Practical Use Cases

Setting focus is useful when you want to ?

  • Direct user attention to a specific input field
  • Create logical tab sequences in forms
  • Automatically focus the first field when a window opens
  • Move focus after validation errors

Setting Initial Focus

import tkinter as tk

root = tk.Tk()
root.geometry("300x150")

# Create an Entry widget
username_entry = tk.Entry(root, width=25)
username_entry.pack(pady=20)

# Set initial focus when window opens
username_entry.focus_set()

tk.Label(root, text="Username field is focused on startup").pack()

root.mainloop()

Comparison

Method Functionality Usage
focus() Sets focus to widget Shorter method name
focus_set() Sets focus to widget More descriptive name

Conclusion

focus() and focus_set() are identical methods that set keyboard focus to a widget. Use either method based on your preference - focus_set() is more descriptive while focus() is shorter.

Updated on: 2026-03-25T23:29:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements