簡體   English   中英

Kivy 自適應調整 TextInput(多行)

[英]Kivy self sizing TextInput (multiline)

晚上好!

當文本進入下一行時,我試圖使 TextInput 小部件增加高度。 問題是,這是在圖像內部,它也必須縮放。 這就是我要說的:

在旁注中,每次我輸入某個文本時,空格都會出現在下一行,如下所示:

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 文件中有問題的部分:

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

這是 minimal.py 文件:

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

如果您知道在圖像中放置文本框的更好方法,請告訴我。 我想到的這個方法似乎有點勉強......

可選問題:是否可以將“.gmd”文件與 kivy 一起使用?

先感謝您!

帶有多行 TextInput 的簡單示例代碼,其高度根據其文本內容動態修改:

textinputtextsize.kv 文件

<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

文件

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

好的,我發現了一些東西。 這似乎不是最好的方法,因為無法再單擊其中的文本,但是如果您找到解決此問題的方法,請告訴我! 好,回到解決方案:

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將自動調整每個換行符上文本框的大小。其外觀如下:

圖片

此外,如果要在一定數量的行后限制大小,可以執行以下操作:

height: (len(usrinp._lines)+1) * usrinp.line_height if (len(usrinp._lines)+1 <= number_of_lines) else number_of_lines * usrinp.line_height

我必須找到一種在其中重新實現滾動條的方法,因為文本只是通過觸摸/單擊拒絕上升。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM