简体   繁体   English

python在gtk textview中搜索文本

[英]python Search for text in a gtk textview

I have looked around and I would think this to be really simple, for some reason I ahve only found parts of what I need. 我环顾四周,我认为这真的很简单,由于某种原因,我只找到了我需要的部分。

I have made a text editor and I have a box that what is typed it will find the problem is that it will only find the first word the in the text view adn I can't get it to search the next line. 我做了一个文本编辑器,然后输入了一个文本框,它会发现问题是它只会在文本视图中找到第一个单词,而我无法搜索下一行。 like a find function in a textdocument. 就像textdocument中的find函数一样。

def search(found):
    search_str = findentry.get_text()
    start_iter =  textbuffer.get_start_iter()
    found =       start_iter.forward_search(search_str,0, None) 
    if found:
      match_start,match_end = found
      textbuffer.select_range(match_start,match_end)

I thought I would be able to do a button that is a search next and make it forward search again adding something and a variable +1. 我以为我可以做一个下一步搜索的按钮,然后再次添加一些内容和变量+1使其向前搜索。 how can I make it search forward and backwards. 如何使它向前和向后搜索。

You are using get_start_iter() , which returns the first position in text buffer. 您正在使用get_start_iter() ,它返回文本缓冲区中的第一个位置。 Probably, you want to start from match_end , which is the position where word ends in the first search, that is, you should start from there. 可能您想从match_end开始,这是单词在第一次搜索中结束的位置,也就是说,您应该从此处开始。

Assuming you are returning found and calling again search with that parameter, then can replace the line: 假设您返回found并使用该参数再次调用search ,则可以替换以下行:

    start_iter =  textbuffer.get_start_iter()

by 通过

    start_iter = found[1] if found else textbuffer.get_start_iter()

The first time, or whenever you want to reset the search, you can pass found=None . 第一次或每当您要重置搜索时,都可以传递found=None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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