Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter Text Area set cursor to end

Tags:

python

tkinter

I have a Tkinter Text() object and I append lines to it using .insert(END, string). When the text fills the available area, I'd expect it to scroll down to show the bottom line of text in the view, but it doesn't scroll (meaning the user has to scroll themselves to see the latest text). I've had a look at the mark_set() method but I can't seem to figure out how to get the cursor to the index of the last item of text.

Any help would be appreciated :)

like image 236
Alex Blundell Avatar asked Oct 08 '13 19:10

Alex Blundell


People also ask

How to set cursor position in Tkinter?

To set the cursor position, you can use the text_widget. mark_set method, with "insert" (or Tkinter. INSERT for a “constant”) first argument and, for the second argument, one of many forms, the most useful being: "%d,%d" % (line, column) , where line is 1-based and column is 0-based.

How to get cursor position in Python Tkinter?

With Tkinter, we can also create a single input field using the Entry widget. Each character in the Entry field that the user enters is indexed. Thus, you can retrieve this index to get the current position of the cursor by using the index() method.

How do you set focus in Python?

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.

What does end do in tkinter?

It doesn't "do" anything. It is a constant, the literal string "end". In this context it represents the point immediately after the last character entered by the user. The function get on a text widget requires two values: a starting position and an ending position.


1 Answers

As usual with Tkinter, there are a number of ways to do this, but one is the easiest: Text.see:

text.insert(END, "spam\n")
text.see(END)

If you want to make sure the start of the new text is visible, not the end of it, or if you only want to do this if the end was already visible beforehand or if the text is active, etc., you may need to look at the other options: scan_mark/scan_dragto, or yview and friends.

like image 194
abarnert Avatar answered Oct 10 '22 20:10

abarnert