- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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()
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.
- How to set focus on Entry widget in Tkinter?
- How to remove focus from a Tkinter widget?
- Setting the focus to a specific Tkinter entry widget
- How to set default text for a Tkinter Entry widget?
- How do I give focus to a python Tkinter text 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 give Tkinter file dialog focus?
- How to center a Tkinter widget?
- 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 make a Tkinter widget invisible?
- Notebook widget in Tkinter