Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I give focus to a python Tkinter text widget?

I'd like to be able to open the App GUI and have it automatically place the cursor into a particular text widget. Best case scenario is: as soon as the app is launched someone can start typing without having to click on the text widget. This is just a small example displaying the issue:

from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
root.mainloop()
like image 615
Symon Avatar asked Mar 18 '11 19:03

Symon


People also ask

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

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.

How do you use a message widget in Python?

The Message widget is used to show the message to the user regarding the behaviour of the python application. The message widget shows the text messages to the user which can not be edited. The message text contains more than one line. However, the message can only be shown in the single font.

Which widget in Tkinter is used for multiple lines of text?

We use the Label widget to display Text or Images in any application. If we want to display a text, we have to assign a value to the text attribute in the constructor. You can also add multiple lines of text in the label widget by using \n next line attribute.


1 Answers

You use the focus_set method. For example:

from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
TextWidget.focus_set()
root.mainloop()
like image 71
Fábio Diniz Avatar answered Sep 19 '22 14:09

Fábio Diniz