Kivy 简明教程

Kivy - Text Input

您经常会看到在桌面和网络应用程序中使用矩形框,用于用户输入一些文本。文本框是任何 GUI 工具包中的一个基本小部件。在 Kivy 中,TextInput 提供了一个控件,用户可以在其中输入和编辑文本。

TextInput 控件可自定义,以接收单行或多行文本。可以使用鼠标选择文本的某个部分。用户还可以使用光标移动在其中执行全屏编辑。

TextInput 类定义在 kivy.uix.textinput 模块中。

from kivy.uix.textinput import TextInput
textbox = TextInput(**kwargs)

以下属性在 TextInput 类中定义 -

  1. allow_copy − 决定是否允许复制文本。allow_copy 是一个 BooleanProperty,默认为 True。

  2. background_color − 背景的当前颜色,格式为 (r, g, b, a)。它是一个 ColorProperty,默认为 [1, 1, 1, 1] (白色)。

  3. border − 用于 BorderImage 图形指令的边框。与 background_normal 和 background_active 一起使用。可用于自定义背景。它必须是四个值的列表:(下,右,上,左)。border 是一个 ListProperty,默认为 (4, 4, 4, 4)。

  4. cursor − 指示当前光标位置的 (col, row) 值的元组。您可以在需要移动光标时设置新的 (col, row)。滚动区域将自动更新,以确保光标在视口中可见。cursor 是一个 AliasProperty。

  5. cursor_color − 光标的当前颜色,格式为 (r, g, b, a)。cursor_color 是一个 ColorProperty,默认为 [1, 0, 0, 1]。

  6. cut() − 将当前选择复制到剪贴板,然后从 TextInput 中删除它。

  7. delete_selection(from_undo=False) − 删除当前文本选择 (如果有)。

  8. disabled_foreground_color − 禁用时前景色,格式为 (r, g, b, a)。disabled_foreground_color 是一个 ColorProperty,默认为 [0, 0, 0, 5] (50% 透明黑色)。

  9. font_name − 要使用的字体的文件名。路径可以是绝对路径或相对路径。相对路径由 resource_find() 函数解析。

  10. font_name − 是一个 StringProperty,默认为 “Roboto”。此值来自 Config。

  11. font_size − 文本的字体大小,以像素为单位。font_size 是一个 NumericProperty,默认为 15 sp。

  12. foreground_color − 前景色,格式为 (r, g, b, a)。foregorund_color 是一个 ColorProperty,默认为 [0, 0, 0, 1] (黑色)。

  13. halign − 文本的水平对齐方式。halign 是一个 OptionProperty,默认为 “auto”。可用的选项为:auto、left、center 和 right。

  14. hint_text - 如果文本是 '',则显示组件的提示文本。hint_text 是一个 AliasProperty,默认为 ''。

  15. hint_text_color - hint_text 文本的当前颜色,格式为 (r, g, b, a),ColorProperty,默认为 [0.5, 0.5, 0.5, 1.0] (灰色)。

  16. input_filter - 如果不为 None,则根据指定模式过滤输入内容。如果为 None,则不应用任何过滤条件。它是一个 ObjectProperty,默认为 None。可能之一为 None、'int'(字符串)、'float'(字符串)或一个可调用对象。

  17. insert_text(substring, from_undo=False) - 在当前光标位置插入新文本。覆盖此函数,以预先处理要进行输入验证的文本。

  18. line_height - 一行的高度。此属性根据 font_name 和 font_size 自动计算。更改 line_height 不会产生任何影响。line_height 是一个 NumericProperty,只读。

  19. line_spacing - 各行之间占据的空间。line_spacing 是一个 NumericProperty,默认为 0。

  20. minimum_height - TextInput 内内容的最小高度。minimum_height 是一个只读 AliasProperty。

  21. multiline - 如果为 True,组件将能够显示多行文本。如果为 False,则“enter”按键按下后将取消 TextInput 的焦点,而不是添加一行新文本。

  22. on_touch_down(touch) - 接收触摸按下事件。touch 参数是 MotionEvent 类的一个对象。如果为 True,则触摸事件的派发将停止。如果为 False,则事件将继续派发到组件树的其余部分。

  23. on_touch_move(touch) - 接收触摸移动事件。touch 位于父坐标中。

  24. on_touch_up(touch) - 接收触摸抬起事件。touch 位于父坐标中。

  25. padding - 文本的填充:[padding_left, padding_top, padding_right, padding_bottom]。填充还接受一个双参数形式 [padding_horizontal, padding_vertical] 和一个单参数形式 [padding]。填充是一个 VariableListProperty,默认为 [6, 6, 6, 6]。

  26. password - 如果为 True,组件将显示其字符为 password_mask 中的字符集。

  27. password_mask - 当 password 为 True 时,设置用于屏蔽文本的字符。password_mask 是一个 StringProperty,默认为 '*'。

  28. paste() - 将来自系统剪贴板的文本插入 TextInput 的当前光标位置。

  29. readonly - 如果为 True,则用户将无法更改 TextInput 的内容。

  30. select_all() - 选择此 TextInput 中显示的所有文本。

  31. select_text(start, end) - 选择此 TextInput 中显示的部分文本。参数有:start - 要从那里开始选择 textinput.text 的索引,以及 end - 要显示选择内容的 textinput.text 的索引。

  32. selection_color - 选择的当前颜色,格式为 (r, g, b, a)。

  33. selection_from - 如果选择正在进行中或已完成,则此属性将表示选择开始的光标索引。

  34. selection_text - 当前内容选择。selection_text 是一个 StringProperty 且默认为 '',只读。

  35. tab_width - 默认情况下,每个制表符将在文本输入小组件上替换为四个空格。您可以设置较小或较大的值。tab_width 是一个 NumericProperty 且默认为 4。

  36. text - 小组件的文本。它是一个 AliasProperty。

Usage

要创建一个简单的 hello world -

widget = TextInput(text='Hello world')

如果您想使用 unicode 字符串创建小组件,请使用 -

widget = TextInput(text=u'My unicode string')

当用户在 TextInput 小组件内输入数据时,它将变成 text 属性的值。

当 TextInput 对象的 text 属性更改时,您可以调用回调。

def callback(instance, value):
   print('The widget', instance, 'have:', value)

textinput = TextInput()
textinput.bind(text=callback)

当 multiline 属性为 False 时,TextInput 接受单行输入。当用户按下 Enter 键时,会生成 on_text_validate 事件 -

def callback(instance, value):
   print('The widget', instance, 'have:', value)

textinput = TextInput(multiline=False)
textinput.bind(on_text_validate=callback)

Example

让我们使用上述 TextInput 类的某些属性和方法。在以下示例中,我们在 BoxLayout 中布置了两个多行文本框和两个按钮。

“COPY”按钮调用 gettext() 方法,该方法会存储从上文本框中选择的文本。

def gettext(self, instance):
   mydemoapp.text = self.text1.selection_text

PASTE 按钮调用回调 insert(),它将选定的文本粘贴到光标位置。

def insert(self, instance):
   self.text2.insert_text(mydemoapp.text)

这两个函数绑定到两个按钮 −

self.b1=Button(text='COPY')
self.b1.bind(on_press=self.gettext)
self.b2=Button(text='PASTE')
self.b2.bind(on_press=self.insert)

build() 方法组装文本框和按钮。

这是 complete code

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.config import Config

Config.set('graphics', 'width', '720')
Config.set('graphics', 'height', '300')
Config.set('graphics', 'resizable', '1')

class mydemoapp(App):
   text=''
   def gettext(self, instance):
      mydemoapp.text = self.text1.selection_text
   def insert(self, instance):
      self.text2.insert_text(mydemoapp.text)

   def build(self):
      main= BoxLayout(orientation= 'vertical')
      self.text1 = TextInput(multiline=True, font_size=20)
      btns = BoxLayout(orientation='horizontal')

      self.b1=Button(text='COPY')
      self.b1.bind(on_press=self.gettext)
      self.b2=Button(text='PASTE')
      self.b2.bind(on_press=self.insert)
      self.text2 = TextInput(
         multiline=True, font_size=20,
         foreground_color=[0,0,1,1]
      )

      btns.add_widget(self.b1)
      btns.add_widget(self.b2)
      main.add_widget(self.text1)
      main.add_widget(btns)
      main.add_widget(self.text2)
      return main

mydemoapp().run()

Output

kivy text input