- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Change the focus from one Text widget to another in Tkinter
In various applications, tkinter widgets are required to be focused to make them active. Widgets can also grab focus and prevent the other events outside the bounds. To manage and give focus to a particular widget, we generally use the focus_set() method. It focuses the widget and makes them active until the termination of the program.
Example
In the following example, we have created two text widgets and we will change the focus from one text widget to another text widget simultaneously using a button widget. Thus, changing the focus is easy by defining two methods which can be handled through the button widgets.
#Import tkinter library from tkinter import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("750x250") #Define a function for changing the focus of text widgets def changeFocus(): text1.focus_set() button.config(command=change_focus, background= "gray71") def change_focus(): text2.focus_set() button.configure(command= changeFocus) #Create a Text WIdget text1= Text(win, width= 30, height= 5) text1.insert(INSERT, "New Line Text") #Activate the focus text1.focus_set() text1.pack() #Create another text widget text2= Text(win, width= 30, height=5) text2.insert(INSERT,"Another New Line Text") text2.pack() #Create a Button button= Button(win, text= "Change Focus",font=('Helvetica 10 bold'), command= change_focus) button.pack(pady=20) win.mainloop()
Output
Running the above code will display a window that contains two text widgets. Initially the “text1” widget will have active focus.
Now click the “Change Focus” button to switch the focus between two text widgets.
- Related Articles
- How to remove focus from a Tkinter widget?
- How do I give focus to a python Tkinter text widget?
- How to erase everything from the Tkinter text widget?
- How to set focus for Tkinter widget?
- How to get the input from the Tkinter Text Widget?
- How to change the color of certain words in a Tkinter text widget?
- Setting the focus to a specific Tkinter entry widget
- How to set focus on Entry widget in Tkinter?
- How to erase everything from a Tkinter text widget?
- How to force Tkinter text widget to stay on one line?
- How to highlight text in a tkinter Text widget?
- Python Tkinter – How do I change the text size in a label widget?
- Underline Text in Tkinter Label widget
- Dynamically change the widget background color in Tkinter
- How to make the Tkinter text widget read only?
