Python KIVY:如何在不点击鼠标的情况下选择TextInput?

时间:2018-05-20 09:49:12

标签: python kivy textinput

我想制作文本输入选择器(以便光标在其上闪烁),而不是单击文本输入本身而是单击按钮。怎么这么容易?

的.py:

import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class Main(Screen):

    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)

    def select_textinput(self, instance):
        pass


class GUI(App):

    def build(self):
        sm = ScreenManager()
        sm.add_widget(Main())
        return sm

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

.kv:

#: kivy 1.10.0

<Main>:
    name: "main_screen"

    TextInput:
        id: ti
        text: "Try to put the cursor here without clicking"

    Button:
        text: "Click here to select the text input !"
        on_press: root.select_textinput(ti)

2 个答案:

答案 0 :(得分:0)

只需将焦点设置为True即可。但要保持它闪烁,你必须安排这个功能:

...
from kivy.clock import Clock
from functools import partial

class Main(Screen):

    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)

    def select_textinput(self, instance):
        Clock.schedule_once(partial(self.keep_blinking, instance), .5)

    def keep_blinking(self, instance, *args):
        instance.focus = True

...

答案 1 :(得分:0)

解决方案如下。有关详细信息,请参阅代码段,示例和输出。

  1. 使用select_all()选择TextInput中显示的所有文本。 注意:如果没有文字,则不会选择任何内容。
  2. 使用cursor = (0, 0)设置文本开头的当前光标位置
  3. 使用focus = True将焦点传递给TextInput。
  4. 当关注TextInput时,将取消选择。因此,我们使用Clock.schedule_once()
  5. 延迟文本选择
  6. on_press 替换为kv文件中的 on_release ,因为无论何时释放按钮点击,该按钮都会有焦点。
  7. 在kv文件中添加了可选代码(focus: Truecursor_width: 8
  8. Text Input » API » Note

      

    关注TextInput时取消选择。如果你需要在TextInput聚焦时显示选择,你应该延迟(使用Clock.schedule)调用函数来选择文本(select_all,select_text)。

    片段

    Python脚本

    def select_textinput(self, instance):
        print("Button Clicked - Select Text")
        instance.cursor = (0, 0)
        instance.focus = True
        Clock.schedule_once(lambda dt: self.select_text(instance, dt=dt), 0.1)
    
    def select_text(self, instance, dt):
        instance.select_all()
    

    kv文件

    Button:
        text: "Click here to select the text input !"
        on_release: root.select_textinput(ti)
    

    实施例

    main.py

    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.clock import Clock
    
    
    class Main(Screen):
    
        def select_textinput(self, instance):
            print("Button Clicked - Select Text")
            instance.cursor = (0, 0)
            instance.focus = True
            Clock.schedule_once(lambda dt: instance.select_all())
    
    
    class GUI(App):
    
        def build(self):
            sm = ScreenManager()
            sm.add_widget(Main())
            return sm
    
    
    if __name__ == '__main__':
        GUI().run()
    

    gui.kv

    #: kivy 1.10.0
    
    <Main>:
        name: "main_screen"
    
        BoxLayout:
            orientation: 'vertical'
    
            TextInput:
                id: ti
                focus: True
                cursor_width: 8  # Default: 1sp
                text: "Try to put the cursor here without clicking"
    
            Button:
                text: "Click here to select the text input !"
                on_release: root.select_textinput(ti)
    

    输出

    Img01 - App Startup Img02 - Cursor Blinking & Text Selected by click of button