简体   繁体   中英

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 'cross-platform' GUI. I open it on my android and the text is either too small or running off the screen i dont want to wrap it only resize.

What properties of the Label can I set so that it auto adjusts the font to fill the parent height and width (which is not explicitly defined)?

The text on the following Labels is printing at the default font size of 14 instead.

Example:

https://pastebin.com/95qA44QD

code

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


class RootWidget(GridLayout):

    def __init__(self, **kwargs):
        # prevent override
        super(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)


    def checkuser(self, *args):
        pass

    def do_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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM