Discussion:
Tkinter question: position cursor within text widget
Fredrik Lundh
2003-01-05 15:39:59 UTC
Permalink
After loading the contens of a text file into a text widget with something
like: <snip>
content = inf.read()
self.textbox.insert(END, content)
self.textbox.focus_set()
</snip>
I want the insert cursor to be at the home position after the file has
loaded.Is there a simple method to position the cursor at a given index? I
haven't *found* any in the "prevailing" Tkinter sources across the web
yet...
the cursor is represented by a predefined mark called INSERT. to
move it around, use the mark_set method:

self.textbox.mark_set(INSERT, 1.0)

more information here:

http://www.pythonware.com/library/tkinter/introduction/text.htm
(click on "Concepts" and scroll down to "Marks")

</F>
v.wehren
2003-01-05 18:39:32 UTC
Permalink
Fredrik,

I tried that, and this works (although I thought it didn't, hence my
post..). But my guess is I have to *workaround* a different problem (sorry I
didn't catch it as such at first): What happens is that INSERT is set to the
mouse cursor position when I open the infile from an
tkFileDialog.askopenfile (on win32), by double-clicking the file name. The
open file ialog may namely overlap the textbox (depending on the position of
the main window). So when I *double-click* the file name to open a file, it
propably also triggers a mouse click at the underlying textbox position
setting the insert cursor to the CURRENT mark. My guess ?s to workaround
this I have to fire mark_set(INSERT, 1.0) from a virtual event again under
this condition...(?)

Vincent Wehren
















"Fredrik Lundh" <fredrik at pythonware.com> schrieb im Newsbeitrag
Post by Fredrik Lundh
After loading the contens of a text file into a text widget with something
like: <snip>
content = inf.read()
self.textbox.insert(END, content)
self.textbox.focus_set()
</snip>
I want the insert cursor to be at the home position after the file has
loaded.Is there a simple method to position the cursor at a given index? I
haven't *found* any in the "prevailing" Tkinter sources across the web
yet...
the cursor is represented by a predefined mark called INSERT. to
self.textbox.mark_set(INSERT, 1.0)
http://www.pythonware.com/library/tkinter/introduction/text.htm
(click on "Concepts" and scroll down to "Marks")
</F>
v.wehren
2003-01-05 11:35:50 UTC
Permalink
I have a small Tkinter question:

After loading the contens of a text file into a text widget with something
like: <snip>
content = inf.read()
self.textbox.insert(END, content)
self.textbox.focus_set()
</snip>

I want the insert cursor to be at the home position after the file has
loaded.Is there a simple method to position the cursor at a given index? I
haven't *found* any in the "prevailing" Tkinter sources across the web
yet...

Anybody?

Thanx!
Vincent Wehren

Loading...