gpt4 book ai didi

python - 在 gtk.TextView 中查找文本

转载 作者:太空狗 更新时间:2023-10-30 02:50:29 25 4
gpt4 key购买 nike

我有一个 gtk.Textview。我想以编程方式查找并选择此 TextView 中的一些文本。我有这段代码,但它无法正常工作。

search_str =  self.text_to_find.get_text()
start_iter = textbuffer.get_start_iter()
match_start = textbuffer.get_start_iter()
match_end = textbuffer.get_end_iter()
found = start_iter.forward_search(search_str,0, None)
if found:
textbuffer.select_range(match_start,match_end)

如果找到文本,则它会选择 TextView 中的所有文本,但我需要它来仅选择找到的文本。

最佳答案

start_iter.forward_search 返回开始和结束匹配的元组,因此您的 found 变量同时具有 match_startmatch_end在里面

这应该使它工作:

search_str =  self.text_to_find.get_text()
start_iter = textbuffer.get_start_iter()
# don't need these lines anymore
#match_start = textbuffer.get_start_iter()
#match_end = textbuffer.get_end_iter()
found = start_iter.forward_search(search_str,0, None)
if found:
match_start,match_end = found #add this line to get match_start and match_end
textbuffer.select_range(match_start,match_end)

关于python - 在 gtk.TextView 中查找文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2364014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com