KivyMD 如何更改 MDToolbar 标题大小和字体?

2022-01-15 00:00:00 python kivy kivymd

问题描述

我正在尝试学习 KivyMD 工具.所以我创建了 MDToolBar,它的标题在单击一些按钮后发生了变化.我试图优化它的 text_size 但做不到.我已经尝试过 font_type:'H1'、font_size:'8sp'.如果可能的话,我也想改变它的字体.任何人都可以帮我解决这个问题.我在下面分享最少的代码:

Im trying to learn KivyMD tools. So i created MDToolBar and it's title changes after some button clicks. Im trying to optimize it's text_size but can't do it.I already tried font_type:'H1', font_size: '8sp'. Also i want to change it's font if its possible. Can anyone help me about that. Im sharing minimal code below:

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
kv = Builder.load_string("""
<SM>:
    P1:
<P1>:
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: 'TEST'
            font_size: '50sp'
            font_name: 'font1.ttf'
""")
class P1(Screen):
    pass
class SM(ScreenManager):
    pass
class MyApp(MDApp):
    def build(self):
        return SM()
if __name__ == '__main__':
    MyApp().run()


解决方案

from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

from kivymd.app import MDApp

kv = Builder.load_string(
    """
<SM>
    P1:


<P1>

    BoxLayout:
        orientation: 'vertical'

        MDToolbar:
            id: toolbar
            title: 'TEST'
""")


class P1(Screen):
    def __init__(self, **kw):
        super().__init__(**kw)
        Clock.schedule_once(self.set_toolbar_font_name)
        Clock.schedule_once(self.set_toolbar_font_size)

    def set_toolbar_font_name(self, *args):
        self.ids.toolbar.ids.label_title.font_name = "JetBrainsMono-ExtraBold-Italic.ttf"

    def set_toolbar_font_size(self, *args):
        self.ids.toolbar.ids.label_title.font_size = '50sp'


class SM(ScreenManager):
    pass


class MyApp(MDApp):
    def build(self):
        return SM()


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

相关文章