Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text vertically inside a text input in kv file?

Tags:

python

kivy

I'm trying to center the text of a TextInput vertically in Kivy. But no solution yet.

How can I do a valign for text input in the kv file? Also centering horizontally would be great to know, how to do it.

For labels I have checked the text align example from Kivy and there the alginment is working because there you can use 'valign' and 'halign' to do the alignment, but that's not available for TextInputs.

Maybe a trick with texture_size could help, but I need to check how this works. I have seen such a trick for a label, but I don't know if it works for the TextInput.

Here's my kv code that I have right now:

#: set Buttonheight1 40
BoxLayout:
    size_hint_y: None
    height: Buttonheight1
    Label:
        id: _number_label
        text: "Number:"
        font_size: 10
        size_hint_x: None
        width: 50
        canvas.after:
            Color:
                rgba: 1,0,0,.5
            Rectangle:
                pos: self.pos
                size: self.size
        TextInput:
            multiline: False
            size_hint_y: None
            height: _number_label.height
            #padding_top: 10
            font_size: 10
            text: str(self.font_size)
            #text: '%s, %s' % (self.get_center_x(), self.get_center_y()) #position test

Explanation of the kv code:

  • Buttonheight1 is a constant with kv set
  • Canvas.after is just for debugging the size of the label
  • The text of the text input shows font size as dummy text

Maybe it's simple to fix but I'm pretty new to Kivy and haven't found an example for this.

Here is how it looks like at the moment:

Screenshot TextInput

(Note: The OK button in the screenshot is not in the kv code above)

like image 786
AWolf Avatar asked May 01 '14 09:05

AWolf


2 Answers

Looking at the api, all I could suggest is that you could try using padding, since you can specify:

Padding of the text: [padding_left, padding_top, padding_right, padding_bottom].

padding also accepts a two argument form [padding_horizontal, padding_vertical] and a one argument form [padding].

Maybe usingpadding_top and/or padding_bottom for instance you could center the text vertically.

like image 102
Totem Avatar answered Oct 19 '22 20:10

Totem


As already suggested in the comments by AWolf. This seems to work best:

padding: [0, (self.height-self.line_height)/2]
like image 7
PalimPalim Avatar answered Oct 19 '22 20:10

PalimPalim