Set cursor position in a Text widget

Wayne Werner picture Wayne Werner · Jul 9, 2010 · Viewed 17.9k times · Source

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.

Answer

jsbueno picture jsbueno · Jul 9, 2010

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: http://effbot.org/tkinterbook/text.htm