【问题标题】:Disabling text wrapping in TextInput在 TextInput 中禁用文本换行
【发布时间】:2019-03-29 14:46:19
【问题描述】:

有没有办法在 TextInput 小部件中禁用文本换行? 也就是说,我仍然希望有换行符,但我不想在段落中换行。 所以看来multiline=False 不是我要找的东西

更新:我的意思是 Windows(例如 Windows 7)Microsoft 记事本(格式 - 自动换行)中有“自动换行”选项。我想在 kivy TextInput 中禁用此选项

【问题讨论】:

  • 你想在TextInput中水平滚动吗?
  • @john-anderson 我的意思是 Windows 中有“自动换行”选项(例如 Windows 7)Microsoft 记事本(格式 - 自动换行)。我想在 kivy TextInput 中禁用此选项
  • 那么当您输入的行长于TextInput 的宽度时,您希望TextInput 显示什么?
  • 该行继续向右,您可以在此处导航到右侧并返回到左侧(到行首),就像 Windows 记事本可以做的那样

标签: python kivy


【解决方案1】:

我不使用 Windows,但这对我来说听起来像是水平滚动。如果您将multiline 设置为False,TextInput 默认会进行水平滚动,但当multiline 设置为True 时则不会。因此,当multiline 为 True 时,可以将 TextInput 放入 ScrollView 中以提供水平滚动:

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.textinput import TextInput


class MyTextInput(TextInput):
    minimum_width = NumericProperty(1)

    def on_cursor(self, instance, newPos):
        # determine scroll position of parent ScrollView if multiline is True
        if not (isinstance(self.parent, ScrollView) and self.multiline):
            return super(MyTextInput, self).on_cursor(instance, newPos)
        if newPos[0] == 0:
            self.parent.scroll_x = 0
        else:
            over_width = self.width - self.parent.width
            if over_width <= 0.0:
                return super(MyTextInput, self).on_cursor(instance, newPos)
            view_start = over_width * self.parent.scroll_x
            view_end = view_start + self.parent.width
            offset = self.cursor_offset()
            desired_view_start = offset - 5
            desired_view_end = offset + self.padding[0] + self.padding[2] + self.cursor_width + 5
            if desired_view_start < view_start:
                self.parent.scroll_x = max(0, desired_view_start / over_width)
            elif desired_view_end > view_end:
                self.parent.scroll_x = min(1, (desired_view_end - self.parent.width) / over_width)
        return super(MyTextInput, self).on_cursor(instance, newPos)

    def on_text(self, instance, newText):
        # calculate minimum width
        width_calc = 0
        for line_label in self._lines_labels:
            width_calc = max(width_calc, line_label.width + 20)   # add 20 to avoid automatically creating a new line
        self.minimum_width = width_calc


theRoot = Builder.load_string('''
ScrollView:
    id: scroller
    effect_cls: 'ScrollEffect'  # keeps from scrolling to far
    MyTextInput:
        size_hint: (None, 1)
        width: max(self.minimum_width, scroller.width)
''')

class TI_in_SV(App):
    def build(self):
        return theRoot

TI_in_SV().run()

请注意,MyTextInput 扩展了 TextInput

【讨论】:

  • 有趣的解决方案!您是否知道在键入内容或在行尾移动光标时如何摆脱从行首(到左边缘)的跳跃?我在 Linux (ubuntu) 上遇到了这个问题
  • 我已经修改了我的答案,试图消除“跳跃”。
猜你喜欢
  • 2021-08-05
  • 2019-09-23
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 2012-08-30
  • 1970-01-01
相关资源
最近更新 更多