From: fredrik at pythonware.com (Fredrik Lundh)
Date: Thu, 29 Apr 1999 15:30:51 GMT
Subject: Q on Tkinter Scrollbar
References: <370CFAC8.32EB0B29@ingr.com> <7g2g0f$km7$1@mlv.mit.edu> <3726C342.1DF7C6D6@ug.cs.usyd.edu.au>
Message-ID: <013201be9256$07c02bf0$f29b12c2@pythonware.com>
Content-Length: 1462
X-UID: 481                                                  

Bradley Baetz wrote:
> Is it possible to find out the location of the top and bottom character
> of a tkinter text object currently showing? I want to be able to seardch
> for a tag on the currently viewable portion, but I can find out the
> start and end positions of the text.

you can use the "@x,y" index syntax for this; the
start of the visible text is "@0,0", the end is "@x,y"
where x and y is the window size (or anything
larger, like "@10000,10000").

also see the attached example.

</F>


#
# tk053.py -- find visible region of a text widget
#
# fredrik lundh, may 1999
#
# fredrik at pythonware.com
# http://www.pythonware.com
#

from Tkinter import *

root = Tk()

# create a scrolled text widget

frame = Frame(root, bd=2, relief=SUNKEN)

scrollbar = Scrollbar(frame, orient=VERTICAL)
scrollbar.pack(fill=Y, side=RIGHT)

text = Text(frame, bd=0)
text.pack()

for i in range(1, 1000):
    text.insert(END, "line %s\n" % i)

# attach scrollbar
text.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=text.yview)

frame.pack()

label = Label(root)
label.pack()

def report_position():

    # get (beginning of) first visible line
    top = text.index("@0,0")

    # get (end of) last visible line
    bottom = text.index("@0,%d" % text.winfo_height())

    # update the label
    label.config(text="top:    %s\nbottom: %s" % (top, bottom))
    root.after(100, report_position) # update 10 times a second

report_position()

mainloop()





