python - 如何在标签或按钮内设置 kivy 字体大小,使其填充标签或按钮,甚至标签或按钮大小发生变化

标签 python kivy kivy-language kivymd

我正在尝试构建一个带有按钮的应用程序(类似于计算器),一切都很好,直到我尝试使应用程序窗口更薄,文本超出了按钮的边框。 我尝试 font_size: self.width/5 来根据屏幕尺寸更改字体,但它在一种情况下有效(宽度或高度)我还找到了一个代码

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.label import Label


class MyLabel(Image):
    text = StringProperty('')

    def on_text(self, *_):
        # Just get large texture:
        l = Label(text=self.text)
        l.font_size = '1000dp'  # something that'll give texture bigger than phone's screen size
        l.texture_update()
        # Set it to image, it'll be scaled to image size automatically:
        self.texture = l.texture


class RootWidget(BoxLayout):
    pass


class TestApp(App):
    def build(self):
        return MyLabel(text='Test test test')


if __name__ == '__main__':
    TestApp().run()

解决了这个问题,但它在 kivy 中使用 Image,我不知道如何在我的情况或 kv 文件中使用它。

目前的文字是这样的:
Problem image

谢谢!

最佳答案

您可以使用kivy.core.text.Label计算渲染文本的大小,并调整字体大小以使文本适合Label。这是一个执行此操作的自定义 Label 类:

from kivy.core.text import Label as CoreLabel
from kivy.core.text.markup import MarkupLabel

def MyLabel(Label):
    # this Label automatically adjusts font size to fill the Label
    def on_text(self, instance, new_text):
        self.adjust_font_size()

    def on_size(self, instance, new_size):
        self.adjust_font_size()

    def adjust_font_size(self):
        font_size = self.font_size
        while True:
            # this loops reduces font size if needed
            if self.markup:
                cl = MarkupLabel(font_name=self.font_name, font_size=font_size, text=self.text)
            else:
                cl = CoreLabel(font_name=self.font_name, font_size=font_size, text=self.text)
            cl.refresh()
            if font_size > self.height - self.padding_y * 2:
                font_size = self.height - self.padding_y * 2
            elif cl.content_width > self.width - self.padding_x * 2 or \
                    cl.content_height > self.height - self.padding_y * 2:
                font_size *= 0.95
            else:
                break
        while True:
            # this loop increases font size if needed
            if self.markup:
                cl = MarkupLabel(font_name=self.font_name, font_size=font_size, text=self.text)
            else:
                cl = CoreLabel(font_name=self.font_name, font_size=font_size, text=self.text)
            cl.refresh()
            if cl.content_width * 1.1 < self.width - self.padding_x * 2 and \
                    cl.content_height * 1.1 < self.height - self.padding_y * 2:
                font_size *= 1.05
            else:
                break

        self.font_size = font_size

关于python - 如何在标签或按钮内设置 kivy 字体大小,使其填充标签或按钮,甚至标签或按钮大小发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72624644/

相关文章:

python - unboundlocalerror 局部变量 'i' 在赋值之前引用

python - Django non-rel 无法将数据库与 mongo 2.4.2 同步

python - 如何在 Kivy 中输入文本后调用函数

python - Kivy - UI 上的标签未更新

python - 使用 kivy.logger 记录变量的值

python - 当 .py 在 RASPBIAN STRETCH LITE 上启动时运行时,kivy 上的鼠标光标隐藏

python - 为什么你可以在字符串上重载 __radd__ 而不是 __rmod__ ?

python - JSON 对象必须是 str、bytes 或 bytearray,而不是 dict

Python/Kivy : conditional design in . kv 文件

python - 如何将微调器值设置为等于 kivy 中 python 方法的输出列表