Search code examples
pythontkintercursor-position

Set cursor position in a Text widget


Is it possible to set the cursor position in a Tkinter Text widget? I'm not finding anything terribly useful yet.

The best I've been able to do is emit a <Button-1> and <ButtonRelease-1> event at a certain x-y coordinate, but that is a pixel amount, not a letter amount.


Solution

  • If "text", "line", and "column" are your text object, the desired text line and desired column variables are, respectively:

    text.mark_set("insert", "%d.%d" % (line + 1, column + 1))
    

    If you would not like to care about the line number... well, you have to.

    Complete documentation at The Tkinter Text Widget.