- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set focus for Tkinter widget?
Tkinter has many inbuilt functions or methods that can be used to extend the functionality of any widget in the application. Sometimes, we need to change or modify the focus of any widget in the application which can be achieved by using the focus_set() method.This method sets the default focus for any widget and makes it active till the execution of the program.
Example
In this example, we will set the focus on first Entry widget where we are required to Enter the name of the User.
#Import the required Libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame win = Tk() #Set the geometry of Tkinter frame win.geometry("750x350") #Define a function to submit the validate the value of Entry widget def submit_name(): Label(frame, text="Hello "+ entry1.get(), font=('Helvetica',12, 'bold')).pack(pady=20) Label(frame, text= "Your Email is : "+ entry2.get(), font=('Helvetica',12, 'bold')).pack(pady=12) #Creates a Frame frame = LabelFrame(win, width= 400, height= 180, bd=3) frame.pack() #Create an Entry widget in the Frame for Accepting the Username entry1 = ttk.Entry(frame, width= 40) entry1.insert(INSERT, "Enter Your Name") entry1.pack(ipadx= 30, ipady=30) #Set the focus on Entry1 entry1.focus_set() #Create an Entry Widget to accept the email Address of the User entry2 = ttk.Entry(frame, width= 40) entry2.insert(INSERT, "Enter Your Email") entry2.pack(pady=10) #Create a submit button submit= ttk.Button(win, text= "submit",command=submit_name) submit.pack(pady=10) win.mainloop()
Output
Run the above code to display two entry widget which has default focus set during the execution of the program.
In the given output, enter the name and Email Address to display the message on the screen.
- Related Articles
- How to set focus on Entry widget in Tkinter?
- How to remove focus from a Tkinter widget?
- How to set default text for a Tkinter Entry widget?
- How do I give focus to a python Tkinter text widget?
- Setting the focus to a specific Tkinter entry widget
- Change the focus from one Text widget to another in Tkinter
- How do you check if a widget has a focus in Tkinter?
- How to set a widget's size in Tkinter?
- How to set the font size of Entry widget in Tkinter?
- How to set the width of a Tkinter Entry widget in pixels?
- How to set the height/width of a Label widget in Tkinter?
- How to center a Tkinter widget?
- How to make a Tkinter widget invisible?
- How to give Tkinter file dialog focus?
- How can I set the default value of my Tkinter Scale widget slider to 100?

Advertisements