Python kivy - 如何减少 TextInput 的高度

Ber*_*one 6 python user-interface kivy kivy-language

我正在使用 kivy 为应用程序制作一个非常简单的 gui。没有什么复杂的,非常简单的布局。

尽管如此,我在使用 TextInputs 时遇到了困难......它们总是以全高显示,我无法使它们调整为“合理”的文本高度,例如高度。

我正在使用 kv 文件样式,因为我发现将它集成到现有应用程序中更简洁、更容易……我想尽可能减少应用程序的 gui-python 代码。

这是我为 TextInput 得到的(无用添加 gui 的其他部分)。

Python代码

# textInput.py
from kivy import require
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder

Builder.load_file('path/to/kv/file/textInput.kv')

require('1.10.0')

class MainScreen(BoxLayout):
    pass

class Test(App):
    def build(self):
        self.title = 'Testing textInput'
        return MainScreen()

if __name__ == '__main__':
    Test().run()
Run Code Online (Sandbox Code Playgroud)

KV代码

# textInput.kv
<MainScreen>
    orientation: 'vertical'

    # Third section title
    Label:
        size_hint: (1, .1)
        text: 'Setup Connection'
        font_size: 25

    # Third section Box
    BoxLayout:
        size_hint: (1, .2)
        padding: [100, 0, 100, 0]
        BoxLayout:
            Label:
                size_hint: (.2, 1)
                text: 'Host'
            TextInput:
                height: self.minimum_height
                multiline: False
                text: 'localhost'
            Label:
                size_hint: (.2, 1)
                text: ''
            Label:
                size_hint: (.2, 1)
                text: 'Port'
            TextInput:
                size_hint: (.2, 1)
                multiline: False
                text: '502'
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多东西,在这里的代码中我尝试同时使用 size_hint 和 height ......但没有一个工作......

小智 7

要设置小部件的高度,首先将 设置size_hint_yNone,然后您可以将 设置height为您想要的任何值。

TextInput:
    size_hint: (.2, None)
    height: 30
    multiline: False
    text: '502'
Run Code Online (Sandbox Code Playgroud)