简体   繁体   English

如何更改 Kivy 中标签的 font_name 属性?

[英]How to change font_name property for lables in Kivy?

I'm trying to create a label, specify some text for it and use some custom font, using the property "font_name" in the python file, not in kv file.我正在尝试创建一个 label,为其指定一些文本并使用一些自定义字体,使用 python 文件中的属性“font_name”,而不是在 kv 文件中。 here is the code:这是代码:

python code: python 代码:

some_text = bidi.algorithm.get_display(
        arabic_reshaper.reshape(<some_persian_text>))


MDLabel(text=some_text, size_hint=(None, None), font_name="fonts/Shabnam-Bold", size=(300, 100), pos_hint={'center_x': 0.5, 'center_y': .8}))

corresponding kv file:对应的kv文件:

<SomeScreen>:
    name: 'some_screen'
    AnchorLayout:
        id: main_layout
        size: self.width, self.height
        anchor_x: 'center'
        anchor_y: 'top'
        MDToolbar:
            id: main_toolbar
            title: "TITLE"
            # left_action_items: [["menu", lambda x: side_bar.set_state('open')]]
            left_action_items:[['arrow-left', lambda x: root.go_back()]]
            md_bg_color: app.theme_cls.primary_color
            elevation: 15

As you can see, I have set the Toolbar in the kv file and I'm trying to render some other content for it dynamically in python.如您所见,我已经在 kv 文件中设置了工具栏,并尝试在 python 中为其动态呈现一些其他内容。 The text of the label is in Persian(some right-to-left language spoken in Iran).The bidi library alongside arabic_reshaper enable the app to render right-to-left text. label 的文本是波斯语(伊朗使用的一些从右到左的语言)。rabic_reshaper 旁边的 bidi 库使应用程序能够呈现从右到左的文本。 The problem is, when I do this totally in kv file (setting the text in python, creating the label in kv file and setting the text and font_name also in kv file), everything works fine.问题是,当我完全在 kv 文件中执行此操作时(在 python 中设置文本,在 kv 文件中创建 label 并将文本和 font_name 也在 kv 文件中设置),一切正常。 But when I do it in this way(creating and setting the label content and the font in py file), the font does not work properly and shows some nonsense:但是当我这样做时(创建和设置 label 内容和 py 文件中的字体),字体无法正常工作并显示一些废话:

在此处输入图像描述

How can I fix this?我怎样才能解决这个问题? Thank you.谢谢你。

It seems that the problem can be solved using markup tags:似乎可以使用标记标签解决该问题:

'[font = "path/to/font"]your text [/font]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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