Dynamic width of a TextInput

1,155 views
Skip to first unread message

_ Leva7 _

unread,
Mar 26, 2016, 12:34:42 PM3/26/16
to Kivy users support
Hi. I'm working with TextInputs to display some text. So I want to adjust the size of those TextInputs to the size of the text it's displaying. (Those widgets represent messages so the text inside of them can be different) I know that labels can provide the desired behaviour with texture_size, but labels are not an option for me because I want the text to be selectable. Are there ways to achieve the behaviour I want or a potential to implement something like that?
Thanks in advance.

ZenCODE

unread,
Mar 27, 2016, 12:06:00 PM3/27/16
to Kivy users support
Crumbs, I though this would be trivial, but you're right. There is no obvious way to do this via exposed properties. So here is a hack...The TextInput has a hidden '_lines_labels' property. This is a list of Labels, one for each row presumably? You could monitor the size of these to do what you want.

If anyone has a better way to do this, please speak now or forever hug your easter bunny....

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput


class TestApp(App):
def build(self):
text_input = TextInput(pos_hint={'center_x':0.5, 'center_y': 0.5},
size_hint=(0.2, 0.1))
text_input.bind(text=self.text_changed)
float_layout = FloatLayout()
float_layout.add_widget(text_input)
return float_layout

def text_changed(self, text_input, text):
if len(text) > 0:
text_input.size_hint_x = None
text_input.width = text_input._lines_labels[0].width + 5

if __name__ == '__main__':
TestApp().run()

 

_ Leva7 _

unread,
Mar 27, 2016, 1:39:28 PM3/27/16
to Kivy users support
Thank you, man! Works like a charm. Exactly what I've been looking for. :)

суббота, 26 марта 2016 г., 21:34:42 UTC+5 пользователь _ Leva7 _ написал:

Oon-Ee Ng

unread,
Mar 27, 2016, 10:04:33 PM3/27/16
to kivy-...@googlegroups.com
This seems like something which should be 'fixed', really. And I
haven't tested it but you should check whether ZenCODE's code works
for multi-line text inputs where the second line is longer than the
first.
> --
> You received this message because you are subscribed to the Google Groups
> "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

ZenCODE

unread,
Mar 27, 2016, 10:29:04 PM3/27/16
to Kivy users support
@Oon-Ee Ng. Yeah, I really doubt it would work for multi-line - You'd need to scan over all the labels. But then again, resizing to the text should probably be done vertically for multi-lien input. Resizing horizontally does not make sense there.

But do agree there should be a cleaner way to do this. Using hidden methods like this reaches into the implementation details will thus break is the implementation changes...

Oon-Ee Ng

unread,
Mar 27, 2016, 10:52:00 PM3/27/16
to kivy-...@googlegroups.com
Vertically is not an issue as TextInput has minimum_height, I believe.

_ Leva7 _

unread,
Mar 28, 2016, 5:27:40 AM3/28/16
to Kivy users support
@Oon-Ee Ng, Yea, ZenCODE's suggestion will work for multiline, it's pretty straight-forward to find the max-length line


суббота, 26 марта 2016 г., 21:34:42 UTC+5 пользователь _ Leva7 _ написал:
Hi. I'm working with TextInputs to display some text. So I want to adjust the size of those TextInputs to the size of the text it's displaying. (Those widgets represent messages so the text inside of them can be different) I know that labels can provide the desired behaviour with texture_size, but labels are not an option for me because I want the text to be selectable. Are there ways to achieve the behaviour I want or a potential to implement something like that?
Thanks in advance.

_ Leva7 _

unread,
Mar 28, 2016, 1:56:39 PM3/28/16
to Kivy users support
I've experienced a strange problem when doing this. When the TextInput contains letters, the width of the _line_labels is correct. However, the uncommon unicode chars (such as this "☺") are ignored and they go beyond the view. Why is that happening? What makes them different from labels or numbers? (Also, when I use a font that doesn't have these symbols and replaces them with boxes, the width also works fine)


суббота, 26 марта 2016 г., 21:34:42 UTC+5 пользователь _ Leva7 _ написал:
Hi. I'm working with TextInputs to display some text. So I want to adjust the size of those TextInputs to the size of the text it's displaying. (Those widgets represent messages so the text inside of them can be different) I know that labels can provide the desired behaviour with texture_size, but labels are not an option for me because I want the text to be selectable. Are there ways to achieve the behaviour I want or a potential to implement something like that?
Thanks in advance.

Oon-Ee Ng

unread,
Mar 28, 2016, 10:23:07 PM3/28/16
to kivy-...@googlegroups.com
That behaviour looks like a bug, you should raise it. UTF-8 formatting etc. is hard to get right, so it may not be an easy fix, but raising an issue (with a code example) is the best start towards that.

--

ZenCODE

unread,
Mar 29, 2016, 12:42:46 PM3/29/16
to Kivy users support
Wonder if it's not related to bearnun's issue? https://github.com/kivy/kivy/issues/3951
Reply all
Reply to author
Forward
0 new messages