Setting global font size in kivy

白昼怎懂夜的黑 提交于 2020-01-03 09:23:11

问题


What is the preferred way, whether through python or the kivy language, to set the global font size (i.e. for Buttons and Labels) in kivy?

What is a good way to dynamically change the global font size setting in proportion to the size of the window?


回答1:


Use a template to create your custom Label:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget 
from kivy.properties import ObjectProperty, NumericProperty

kv = '''
[MyLabel@Label]:
    text: ctx.text if hasattr(ctx, 'text') else ''
    font_size: 24
    markup: True

<MyWidget>:
    id: f_wid
    BoxLayout:
        size: f_wid.size
        orientation: 'vertical'
        MyLabel:
            text: "Hello world 1"
        MyLabel:
            text: "Hello world 2"
        MyLabel:
            text: "Hello world 3"
        MyLabel:
            text: "Hello world 4"   
        MyLabel:
            text: "Hello world 1"
        MyLabel:
            text: "Hello world 2"
        MyLabel:
            text: "Hello world 3"
        MyLabel:
            text: "Hello world 4"   
'''
Builder.load_string(kv)

import kivy
kivy.require('1.7.1') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.widget import Widget

class MyWidget(Widget):
    pass

class MyApp(App):
    def build(self):
        return MyWidget()

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

To have font size depended on screen size, instead of using fixed values calculate it using self.heigh:

[MyLabel@Label]:
    text: ctx.text if hasattr(ctx, 'text') else ''
    font_size: self.height/2
    markup: True

UPDATE

Alternative approach is setting variable using #:set syntax:

kv = '''
#:set default_font_size "36sp"
<MyWidget>:
    id: f_wid
    BoxLayout:
        size: f_wid.size
        orientation: 'vertical'
        Label:
            text: "Hello world 1"
            font_size: default_font_size
        Label:
            text: "Hello world 2"
            font_size: default_font_size
        Label:
            text: "Hello world 3"
            font_size: default_font_size
        Label:
            text: "Hello world 4"   
            font_size: default_font_size
        Label:
            text: "Hello world 1"
            font_size: default_font_size
        Label:
            text: "Hello world 2"
            font_size: default_font_size
        Label:
            text: "Hello world 3"
            font_size: default_font_size
        Label:
            text: "Hello world 4"   
            font_size: default_font_size
'''
Builder.load_string(kv)



回答2:


<Label>:
    font_size: dp(20)
    font_name: 'path/to/funcy/font.ttf'

Will set the font name and the font size globally for any widget that uses Label as it's base(TextInput and a few other widgets don't).




回答3:


I know this question is old but you did ask about "dynamically change the global font size setting in proportion to the size of the window"

For a similar problem I've created AutoSizedLabel

class TestApp(App):
    def build(self):
        return AutoSizedLabel(text="crazy stuff", ratio=0.5)

It is pip install-able by :

pip install kivyoav


来源:https://stackoverflow.com/questions/19052905/setting-global-font-size-in-kivy

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