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 :)
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With