簡體   English   中英

滾動到Tkinter中的文本

[英]Scroll to text in Tkinter

大家好,我有一個文本框,我在其中搜索字符串並突出顯示它們,效果很好。 問題是測試框很長,用戶可能需要滾動幾分鍾才能找到突出顯示的字符串。 我正在尋找一種設置滾動條位置的方法,以使突出顯示的字符串的首次出現在頂部。 我希望這是我的突出顯示功能。

    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')

我認為這將與

self.scrollbar.set(float(idx))

無需做滾動條運算:

self.dp_text.see(idx)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM