【问题标题】:How to select text in TextBox如何在文本框中选择文本
【发布时间】:2018-07-21 06:57:03
【问题描述】:

我正在使用python-2.7kivy。有人帮助我,当我点击TextBox 时,如何使用python 或kivy 代码选择文本?

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 50)

class abc(BoxLayout):
    pass

class Test(App):
    def build(self):
        return abc()


if __name__ == '__main__':
    Test().run()

test.kv

<abc>:
    BoxLayout:
        orientation: "vertical"
        size_hint_y: .5

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size_hint_x: .6

            Label:
                text: "TEXT"
                text_size: self.size
                valign: 'middle'
                size_hint_x: .2

            TextInput:
                size_hint_x: .4

【问题讨论】:

    标签: python-2.7 kivy kivy-language


    【解决方案1】:

    您必须在 select_all() 旁边使用 on_touch_down,如下所示:

    #:import Clock kivy.clock.Clock
    
    <abc>:
        BoxLayout:
            orientation: "vertical"
            size_hint_y: .5
    
            BoxLayout:
                orientation: "horizontal"
                spacing: 10, 10
                size_hint_x: .6
    
                Label:
                    text: "TEXT"
                    text_size: self.size
                    valign: 'middle'
                    size_hint_x: .2
    
                TextInput:
                    size_hint_x: .4
                    on_touch_down: Clock.schedule_once(lambda dt: self.select_all())
    

    您可以通过类似的方式从 python 中进行操作。

    *.py

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.core.window import Window
    from kivy.uix.textinput import TextInput
    from kivy.clock import Clock
    
    Window.clearcolor = (0.5, 0.5, 0.5, 1)
    Window.size = (400, 50)
    
    class abc(BoxLayout):
        pass
    
    class MyTextInput(TextInput):
        def on_touch_down(self, touch):
            Clock.schedule_once(lambda dt: self.select_all())
            TextInput.on_touch_down(self, touch)
    
    class Test(App):
        def build(self):
            return abc()
    
    
    if __name__ == '__main__':
        Test().run()
    

    *.kv

    <abc>:
        BoxLayout:
            orientation: "vertical"
            size_hint_y: .5
    
            BoxLayout:
                orientation: "horizontal"
                spacing: 10, 10
                size_hint_x: .6
    
                Label:
                    text: "TEXT"
                    text_size: self.size
                    valign: 'middle'
                    size_hint_x: .2
    
                MyTextInput:
                    size_hint_x: .4
    

    【讨论】:

    • 如果这个MyTextInputclass有多个实例,那么每个实例都会被高亮显示。
    【解决方案2】:

    test.py

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.core.window import Window
    from kivy.uix.textinput import TextInput
    from kivy.clock import Clock
    
    Window.clearcolor = (0.5, 0.5, 0.5, 1)
    Window.size = (400, 50)
    
    class abc(BoxLayout):
        pass
    
    class MyTextInput(TextInput):
        def on_focus(self, instance, isFocused):
            if isFocused:
                Clock.schedule_once(lambda dt: self.selected_text())
    
        def selected_text(self):
            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)
            Clock.schedule_once(lambda dt: self.select_text(ci - start, ci + end))
    
    
    class Test(App):
        def build(self):
            return abc()
    
    
    if __name__ == '__main__':
        Test().run()
    

    test.kv

    <abc>:
        BoxLayout:
            orientation: "vertical"
            size_hint_y: .5
    
            BoxLayout:
                orientation: "horizontal"
                spacing: 10, 10
                size_hint_x: .6
    
                Label:
                    text: "TEXT"
                    text_size: self.size
                    valign: 'middle'
                    size_hint_x: .2
    
                MyTextInput:
                    size_hint_x: .4
    

    【讨论】:

      【解决方案3】:

      解决方法如下。详情请参考sn-ps、示例和输出。

      1. 使用select_all() 选择TextInput 中显示的所有文本。 注意:如果没有文本,则不会选择任何内容。
      2. TextInput 获得焦点时,选择被取消。因此,我们使用Clock.schedule_once()延迟文本选择

      Text Input » API » Note

      当 TextInput 获得焦点时,选择被取消。如果您需要在 TextInput 获得焦点时显示选择,您应该延迟(使用 Clock.schedule)对用于选择文本的函数(select_all、select_text)的调用。

      片段

      Python 脚本

      def on_focus(self, instance):
          if instance.focus:
              Clock.schedule_once(lambda dt: instance.select_all())
      

      kv 文件

              TextInput:
                  size_hint_x: .4
                  on_focus: root.on_focus(self)
      

      示例

      main.py

      ​​>
      from kivy.app import App
      from kivy.uix.boxlayout import BoxLayout
      from kivy.core.window import Window
      from kivy.clock import Clock
      
      Window.clearcolor = (0.5, 0.5, 0.5, 1)
      Window.size = (400, 50)
      
      
      class abc(BoxLayout):
          def on_focus(self, instance):
              if instance.focus:
                  print("TextInput is focused [focus={}]".format(instance.focus))
                  Clock.schedule_once(lambda dt: instance.select_all())
              else:
                  print("TextInput is defocused [focus={}]".format(instance.focus))
      
      
      class Test(App):
          def build(self):
              return abc()
      
      
      if __name__ == '__main__':
          Test().run()
      

      test.kv

      #:kivy 1.11.0
      
      <abc>:
          BoxLayout:
              orientation: "vertical"
              size_hint_y: .5
      
              BoxLayout:
                  orientation: "horizontal"
                  spacing: 10, 10
                  size_hint_x: .6
      
                  Label:
                      text: "TEXT"
                      text_size: self.size
                      valign: 'middle'
                      size_hint_x: .2
      
                  TextInput:
                      size_hint_x: .4
                      on_focus: root.on_focus(self)
      

      输出

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 2011-02-18
        • 2014-03-03
        • 2011-01-28
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        相关资源
        最近更新 更多