以kivy设置全局字体大小

时间:2013-09-27 14:10:52

标签: python properties kivy font-size

无论是通过python还是kivy语言,在kivy中设置全局字体大小(即按钮和标签)的首选方法是什么?

根据窗口大小动态更改全局字体大小设置的好方法是什么?

3 个答案:

答案 0 :(得分:10)

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

将为使用Label作为基础的任何窗口小部件设置全局字体名称和字体大小(TextInput和其他一些小部件不会)。

答案 1 :(得分:2)

使用template创建自定义标签:

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

要使字体大小取决于屏幕尺寸,而不是使用固定值,请使用self.heigh计算:

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

<强>更新

替代方法是使用#: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 :(得分:0)

我知道这个问题已经过时了,但您确实询问了&#34;与窗口大小成比例地动态更改全局字体大小设置&#34;

对于类似问题,我创建了 AutoSizedLabel

- (void)contextDidChange:(NSNotification *)notification
{
    NSManagedObjectContext *context = notification.object;
}

由pip安装:

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