Skip to content Skip to sidebar Skip to footer

How To Dynamically Resize Label In Kivy Without Size Attribute

So, I get that you can usually just use self(=)(:)texture_size (py,kv) but all of my widgets are either based on screen(root only) or size_hint. I am doing this on purpose for a 'c

Solution 1:

You really should follow the suggestion from @eyllanesc. But here is one way to do what you want (if I am interpreting your question correctly):

from functools import partial

from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.app import runTouchApp
from kivy.uix.textinput import TextInput


classRootWidget(GridLayout):

    def__init__(self, **kwargs):
        # prevent overridesuper(RootWidget, self).__init__(**kwargs)
        self.cols = 1
        self.email_label =  Label(
                color=(1, .5, .5, 1),
                text="Email:",
                size_hint=(1, 1)
            )
        self.add_widget(self.email_label)
        self.email = TextInput(
            text='',
            foreground_color=(1, .5, .5, 1),
            multiline=False,
            size_hint=(1, 1))
        self.add_widget(self.email)
        self.add_widget(
            Label(
                color=(1, .5, .5, 1),
                text="Password:",
                size_hint=(1, 1)))
        self.pw = TextInput(
            text='',
            foreground_color=(1, .5, .5, 1),
            multiline=False,
            password=True,
            size_hint=(1, 1))
        self.add_widget(self.pw)
        self.login = Button(
            color=(1, .5, .5, 1),
            background_color=(0, 0, 0, 1),
            text="Login",
            size_hint=(1, 4))
        self.add_widget(self.login)
        self.login.bind(
            on_press=partial(
                self.checkuser,
                self.email,
                self.pw))

        self.bind(size=self.do_resize)


    defcheckuser(self, *args):
        passdefdo_resize(self, rootWidgt, new_size):
        self.email_label.font_size = 0.025 * new_size[1]

if __name__ == '__main__':
    runTouchApp(RootWidget())

Simply put, save references to the things you want to adjust dynamically, add a binding to call do_resize() whenever your RootWidget is resized, and put code in there to make the adjustments you want. Note that the do_resize method will be called on the first display of RootWidget.

Post a Comment for "How To Dynamically Resize Label In Kivy Without Size Attribute"