alessandro
alessandro

Reputation: 3984

text widget returning cursor

my problem: in a Tkinter editor I'm writing, there is a condition when I need to reload the file and reopen it returning at the point I'm writing.

My 1st reload attempt is this

pos=config.text.index(INSERT)          # memorize where I am
...                                    # do stuff
textopen_and_display(currentfn)        # reopen it
text.mark_set(INSERT,pos)          # go 
text.see(INSERT)                   # there

where text is my text widget. The problem is that the .see() method called this way doesnt do what I want: it scrolls down just enough to see the INSERT, and stops. If I am not in the first lines, where the top of file is visible, it doesnt work.

What I need is the index of the last visible line of the text widget: how can I find it? Using .see() on this index should work the way I want

P.S. I am even unable to find the text widget height at runtime, since I defined it as small size (height=5), and PACKed it with fill=BOTH, expand = 1: asking text.config() for its height it returns the init value of 5

Upvotes: 2

Views: 191

Answers (1)

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

To save your scroll position:

x_pos = text.xview()[0]
y_pos = text.yview()[0] 

To restore the scroll position:

text.xview(Tkinter.MOVETO, x_pos)
text.yview(Tkinter.MOVETO, y_pos)

Upvotes: 2

Related Questions