【问题标题】:Python KIVY: how to select a TextInput without mouse click?Python KIVY:如何在不单击鼠标的情况下选择 TextInput?
【发布时间】:2018-05-20 09:49:12
【问题描述】:

我想制作一个文本输入选择器(以便光标在其上闪烁),而不是单击文本输入本身,而是单击一个按钮。如何轻松做到这一点?

.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)

【问题讨论】:

  • 您必须具体说明您的问题,而不是您询问了 XY problem 导致我浪费时间。更新您的问题,我很乐意再次为您提供帮助

标签: python kivy textinput


【解决方案1】:

只需将焦点设置为 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

...

【讨论】:

    【解决方案2】:

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

    1. 使用select_all() 选择TextInput 中显示的所有文本。 注意:如果没有文本,则不会选择任何内容。
    2. 使用cursor = (0, 0) 将当前光标位置设置在文本的开头
    3. 使用focus = True 将焦点传递给TextInput。
    4. 当 TextInput 获得焦点时,选择被取消。因此,我们使用 Clock.schedule_once() 来延迟文本选择
    5. 将 kv 文件中的 on_press 替换为 on_release,因为只要释放按钮单击,按钮就会获得焦点。
    6. 在 kv 文件中添加了可选代码(focus: Truecursor_width: 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)
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多