[英]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.