如何在不具有size属性的情况下在kivy中动态调整标签的大小 [英] How to dynamically resize label in kivy without size attribute

查看:98
本文介绍了如何在不具有size属性的情况下在kivy中动态调整标签的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我知道您通常可以只使用self(=)(:) texture_size(py,kv),但我所有的小部件都基于screen(仅root)或size_hint.我这样做是为了跨平台" GUI.我在Android设备上将其打开,但文本太小或超出了屏幕尺寸,我不想只调整大小来包装它.

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.

我可以设置Label的哪些属性,以便它自动调整字体以填充父级的高度和宽度(未明确定义)?

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)?

以下标签上的文本以默认字体大小14代替打印.

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

示例:

https://pastebin.com/95qA44QD

code

推荐答案

您确实应该遵循@eyllanesc的建议.但是,这是做您想要的事情的一种方法(如果我正确地解释了您的问题):

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())

简单地放置,保存对要动态调整的内容的引用,添加绑定以在调整RootWidget大小时调用do_resize(),并在其中放置代码以进行所需的调整.请注意,do_resize方法将在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.

这篇关于如何在不具有size属性的情况下在kivy中动态调整标签的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆