从TextInput获取隔离文本

时间:2018-09-01 15:29:38

标签: python user-interface kivy kivy-language

从TextInput小部件中获取所有文本很容易。但是,如果只想在特定位置获得特定部分怎么办?想像一下从段落中的光标位置x直到段落中的光标位置x完全获取文本?

我能够做到这一点,但是我的使用方式看起来像个头,我想知道是否有合适的方法吗? (在文档中找不到合适的功能)

例如:我想获取当前正在输入的单词(但这也可以应用于单词f.e.的搜索/替换等)。因此,我在TextInput的on_double_tap()方法中使用了原始代码,没有最后一部分(我不希望它在视觉上选择单词)。因此,第一,我将selection_color的alpha颜色更改为零(因此它是不可见的),从位置x到x进行了selection_text的显示,取消了选择,并将alpha颜色恢复为默认值。 但这听起来不对,这不是一种Python的做事方式,更是一种a头。有更Pythonic /正确的方式做到这一点吗?

这是我用于选择当前输入的单词并获取字符串的当前代码(这是TextInput小部件的实例类中的一种方法):

#code from on_double_tap()
def select_word(self):
    #Code from the on_double_tap() method inside TextInputwidget
    ci = self.cursor_index()
    cc = self.cursor_col
    line = self._lines[self.cursor_row]
    len_line = len(line)
    start = max(0, len(line[:cc]) - line[:cc].rfind(u' ') - 1)
    end = line[cc:].find(u' ')
    end = end if end > - 1 else (len_line - cc)

    #Now this is the part I added
    #Save de default selection_color before hiding it with alpha 0
    selection_color = self.selection_color
    self.selection_color=[0,0,0,0]
    self.select_text(ci - start, ci + end)
    word = self.selection_text
    self.cancel_selection()
    #Restore default selection_color
    self.selection_color = selection_color
    return(word)

隐藏东西似乎不是执行此操作的最佳方法,是吗?有没有更好的办法? 谢谢

0 个答案:

没有答案