How to dynamically resize label in kivy without size attribute

时光毁灭记忆、已成空白 提交于 2021-01-29 05:04:58

问题


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

回答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


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.



来源:https://stackoverflow.com/questions/52788848/how-to-dynamically-resize-label-in-kivy-without-size-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!