Search code examples
pythonkivytextinputkivy-language

Kivy Python- Text Input Box Filling Entire Float Layout Screen


I am running into an issue with TextInput in Kivy.

When I add it to the existing FloatLayout on one of my screens it takes up the entire window, even with a height specified. I'd like to keep this in the .py file, so please refrain from adding any styling options for sizing in a .kv file.

class WebsiteInput(Screen):
    def __init__(self, **kwargs):
        super(WebsiteInput, self).__init__(**kwargs)
        Clock.schedule_once(self._finish_init)

    def _finish_init(self, dt):
        # Title Label
        self.lbl1 = Label(text="Enter a URL to bind to this button:", pos=(self.x, self.height +132))
        self.lbl1.font_name = 'Montserrat-Bold.ttf'
        self.lbl1.font_size = 28
        self.ids.float_web.add_widget(self.lbl1)
        # URL Text Input
        self.web_input = TextInput(height=100)
        self.web_input.height = 100
        self.ids.float_web.add_widget(self.web_input)

As you can see I have tried affecting the size in two different locations, and it is still filling the entire window.


Solution

  • For the size property to take effect you need the size_hint property to be None on the corresponding axis:

    class WebsiteInput(Screen):
        def __init__(self, **kwargs):
            super(WebsiteInput, self).__init__(**kwargs)
            Clock.schedule_once(self._finish_init)
    
        def _finish_init(self, dt):
            # Title Label
            self.lbl1 = Label(text="Enter a URL to bind to this button:",
                              pos=(self.x, self.height +132))
            self.lbl1.font_name = 'Montserrat-Bold.ttf'
            self.lbl1.font_size = 28
            self.ids.float_web.add_widget(self.lbl1)
            # URL Text Input
            self.web_input = TextInput(height=100, 
                                       size_hint = (1,  None))
            self.ids.float_web.add_widget(self.web_input)