[Tutor] gui tks and encodings

Alan Gauld alan.gauld at blueyonder.co.uk
Sat May 29 18:42:21 EDT 2004

> the 'official' gui toolkit of python, i think it should manage to
solve some
> basic problems, like navigation on wrapped text.. (not mentioning
> cannot move the insertion cursor from program.. or am i wrong?)

I'm not sure what you tried  but moving the insertion point is pretty
easy - once you know that Tk calls the insertion point the "insert

There is a method:

myWidget.mark_set('insert', index)

Where index is the usual Tk style index, typically a line.char
pair. You can also use a mark plus modifier so for example:

>>> tk = Tk()
>>> t = Text()
>>> t.pack()

>>> def move5():
...       t.mark_set("insert","insert + 5 chars")
>>> b = Button(tk,text="move 5",command=move5)
>>> b.pack()
>>> tk.mainloop()

Creates a Text widget and button which moves the
insert cursor 5 characters forward.
(Note the use of strings in mark_set()! - A Tk feature)

The other mark of note is "current" which tracks the mouse
position. If the user clicks the mouse in the text the values of
"current" and "insert" will become the same.

You can of course create your own named marks too and jump
to them etc.

None of this is exactly clear in the Tkinter docs I must say,
but the pages do explain it a little.


But I dsid have to experiment a bit to get the example
above working - even after reading the help!

However it should be obvious that Tkinters Text editing capabilities
are very much present since IDLE is itself built using it! :-)

Alan G.

More information about the Tutor mailing list