简体   繁体   中英

Scroll to text in Tkinter

Hey guys I have a text box that I search through for strings and highlight them which works fine. The issue is the test box is very long and it can take the user several minutes of scrolling to find the string that is highlighted. Im looking for a way to set the location of the scrollbar so that the first occurence of a highlighted string is at the top. I hope this makes sense here is my highlighting function..

    def highlight(self, args):
    idx = '1.0'
    if (args == "clear"):
        self.dp_text.tag_remove('found', '1.0', END)
    if args=="":
        return
    while 1:
        # find next occurrence, exit loop if no more
        idx = self.dp_text.search(args, idx, nocase=1, stopindex=END)
        if not idx: break
        # index right after the end of the occurrence
        lastidx = '%s+%dc' % (idx, len(args))
        # tag the whole occurrence (start included, stop excluded)
        self.dp_text.tag_add('found', idx, lastidx)
        # prepare to search for next occurrence
        idx = lastidx
    self.dp_text.tag_config('found', foreground='red', background='yellow')

I'm thinking it will be something along the lines of

self.scrollbar.set(float(idx))

无需做滚动条运算:

self.dp_text.see(idx)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM