如何在文本框中选择文本

时间:2018-07-21 06:57:04

标签: python-2.7 kivy kivy-language

我正在使用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

3 个答案:

答案 0 :(得分:1)

您必须在public class Transaction { public string Id { get; set; } public int Order { get; set; } public DateTime Time { get; set; } public int Type { get; set; } public decimal Lots { get; set; } public string Symbol { get; set; } public decimal Price { get; set; } public decimal StopLoss { get; set; } public decimal TakeProfit { get; set; } public DateTime? CloseTime { get; set; } public decimal ClosePrice { get; set; } public decimal Swap { get; set; } public decimal Profit { get; set; } [StringLength(255)] public string Comment { get; set; } public decimal Commission { get; set; } public DateTime? Expiration { get; set; } public int MagicNumber { get; set; } public string AccountNumber { get; set; } } 旁边使用o n_touch_down,如下所示:

select_all()

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

*。py

#: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())

*。kv

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

  1. 使用select_all()选择TextInput中显示的所有文本。 注意:如果没有文本,则什么也不会选择。
  2. TextInput 成为焦点时,选择被取消。因此,我们使用Clock.schedule_once()
  3. 延迟文本选择

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)

输出

enter image description here