首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kivy self Kivy(多行)

Kivy self Kivy(多行)
EN

Stack Overflow用户
提问于 2018-01-04 02:49:55
回答 2查看 1.4K关注 0票数 2

晚上好!

我正在尝试在文本进入下一行时增加一个TextInput小部件的高度。问题是,这是在图像中,它也必须缩放。这就是我要说的:

顺便说一句,每次我输入某个文本时,空格就会出现在下一行:

代码语言:javascript
复制
The quick brown fox jumped over   |
the lazy dog.  The quick brown fox|
 jumped over the lazy dog.  The   |
sly brown fox jumped over the lazy|

有没有办法避免这种情况?

下面是file.kv文件中出现问题的部分:

代码语言:javascript
复制
#:kivy 1.10.0

<Manager>:
    Chat:
        name: 'chat'
<Chat>:
    canvas:
        Rectangle:
            pos: self.x, 0
            size: self.width, self.height

    Button:
        id: stgs
        background_down: './icons/settings-press.png'
        background_normal: './icons/settings.png'
        border: 0, 0, 0, 0
        always_release: True
        right: root.right - 20
        top: root.top - 10
        size: 40, 40
        size_hint: None, None
        on_release:
            root.manager.transition.direction = 'down'
            root.manager.current = 'settings'

    Button:
        id: bck
        background_down: './icons/back-press.png'
        background_normal: './icons/back.png'
        border: 0, 0, 0, 0
        x: root.x + 20
        top: root.top - 10
        size: 40, 40
        size_hint: None, None
        on_release:
            root.manager.transition.direction = 'right'
            root.manager.current = 'main'

    BoxLayout:
        orientation: 'horizontal'
        padding: 10, 10, 10, 10
        cols: 2
        Image:
            id: inpimg
            source: './icons/user_inp.png'
            x: root.x + 10
            y: root.y + 10
            size: root.width - 40, 40
            size_hint: 0.9, None
            allow_stretch: True
            keep_ratio: False
            TextInput:
                id: usrinp
                valign: 'middle'
                halign: 'left'
                font_size: 16
                multiline: True
                x: root.ids['inpimg'].x + 10
                y: root.ids['inpimg'].y + 5
                background_color: 0, 0, 0, 0
                size: root.width - 80, 33

        Button:
            id: post
            foreground_color: 0, 0, 0, 0
            background_down: './icons/type1-press.png'
            background_normal: './icons/type1.png'
            border: 0, 0, 0, 0
            size: 40, 40
            x: root.width * 14/17 + 5
            y: root.y + 20
            size_hint: None, None

下面是最小的.py文件:

代码语言:javascript
复制
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen


class Chat(Screen):
    pass

class Manager(ScreenManager):
    pass

class FileApp(App):
    def build(self):
        return Manager()


if __name__ == "__main__":
    FileApp().run()

如果你知道将文本框放入图像中的更好方法,请让我知道!我想到的这个方法看起来有点强迫...

可选问题:可以在kivy中使用'.gmd‘文件吗?

提前谢谢你!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-23 13:52:04

包含多行TextInput的简单示例代码,其高度可根据文本内容动态修改:

textinputtextsize.kv文件

代码语言:javascript
复制
<TextInputTextSize>:
    canvas.before:
        Color:
            rgb: [0.22,0.22,0.22]
        Rectangle:
            pos: self.pos
            size: self.size
    orientation: "vertical"
    textInputSingleLine: txt_input_singleline
    textInputMultiline: txt_input_multiline

    GridLayout:
        cols: 1
        canvas.before:
            Rectangle:
                pos: self.pos
                size: self.size
        TextInput:
            id: txt_input_singleline
            size_hint_y: None
            height: "28dp"
            focus: True
            multiline: False
            on_text_validate: root.submitRequest() # ENTER triggers root.submitRequest()
        TextInput:
            id: txt_input_multiline
            text: "Audio - ET L'UNIVERS DISPARAÎTRA La nature   \nillusoire de notre réalité et le pouvoir \ntranscendant du véritable pardon \n+ commentaires de Gary Renard"
            size_hint_y: None
            height: (len(txt_input_multiline._lines)+1) * txt_input_multiline.line_height

py文件

代码语言:javascript
复制
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout


class TextInputTextSize(BoxLayout):
    textInput = ObjectProperty()
    textInputMultiline = ObjectProperty()

    def submitRequest(self):
        # Get the request from the TextInput
        textInputTxt = self.textInputSingleLine.text
        self.textInputMultiline.text += '\n' + textInputTxt

class TextInputTextSizeApp(App):
    def build(self):
        return TextInputTextSize()

if __name__ == '__main__':
    TextInputTextSizeApp().run()
票数 1
EN

Stack Overflow用户

发布于 2018-01-05 16:29:40

好吧,我发现了些东西。这似乎不是最好的方法,因为里面的文本不能再点击了,但如果你找到了解决这个问题的方法,请让我知道!好的,回到解决方案:

代码语言:javascript
复制
BoxLayout:
    orientation: 'horizontal'
    padding: 10, 10, 10, 10
    cols: 2
    Image:
        id: inpimg
        source: './icons/user_inp.png'
        x: root.x + 10
        y: root.y + 10
        width: root.width - 40
        height: max(40, scrlv.height)
        size_hint: 0.9, None
        allow_stretch: True
        keep_ratio: False
        ScrollView:
            id: scrlv
            x: root.ids['inpimg'].x + 10
            y: root.ids['inpimg'].y
            width: root.width - 80
            height: (len(usrinp._lines)+1) * usrinp.line_height
            TextInput:
                id: usrinp
                valign: 'middle'
                halign: 'left'
                font_size: 16
                multiline: True
                size_hint: 1, None
                height: scrlv.height
                background_color: 0, 0, 0, 0

使用height: (len(usrinp._lines)+1 *usrinp.line_height将自动调整每个换行符上的文本框大小。下面是它的外观:

此外,如果您想限制特定行数之后的大小,您可以执行以下操作:

代码语言:javascript
复制
height: (len(usrinp._lines)+1) * usrinp.line_height if (len(usrinp._lines)+1 <= number_of_lines) else number_of_lines * usrinp.line_height

我必须找到一种方法在这里重新实现滚动条,因为文本只是拒绝通过触摸/点击来向上移动。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48083420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档