7

I am using kivy to make a very simple gui for an application. Nothing complex, very simple layout.

Nevertheless I am having a hard time with TextInputs...They always display with full height and I can't manage to make them adjust to a "reasonable" text-height like height.

I am using the kv files style since I find it cleaner and easier to integrate it in an already existing app...I would like to reduce as much as possible the gui-python code of the app.

Here is what I got for the TextInput (useless to add other parts of the gui).

Python code

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

KV code

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

I have tried lot of stuff, in the code here I am trying both to use size_hint and height...but none works..

DavidG
  • 24,279
  • 14
  • 89
  • 82
Bertone
  • 756
  • 2
  • 9
  • 23

2 Answers2

9

To set a height of a widget, first set the size_hint_y to None and then you can set the height to whatever you want.

TextInput:
    size_hint: (.2, None)
    height: 30
    multiline: False
    text: '502'
Edvardas Dlugauskas
  • 1,469
  • 1
  • 12
  • 14
  • Great thanks, that did the trick I was going nuts. Can I ask you also how I could center vertically the textinput I just managed to reduce in height? – Bertone May 31 '17 at 15:19
  • No worries, kivy is absurdly confusing about the simple things at times. To center vertically (if you are not going to use any other widgets) I recommend using a FloatLayout, for widgets inside it use `pos_hint`. Set `'center_y'` to 0.5, read more here https://kivy.org/docs/api-kivy.uix.widget.html#kivy.uix.widget.Widget.pos_hint – Edvardas Dlugauskas May 31 '17 at 15:34
0

in addition to the answer size_hint_x and size_hint_y must be set to None respectively before the height and width attribute can be used i.e size_hint: (None, None) for less typing. If you want to set the width attribute, size_hint_x is set to None and vice-versa.