在Kivy中设置全局字体大小 [英] Setting global font size in kivy

查看:353
本文介绍了在Kivy中设置全局字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过kivy设置全局字体大小(即用于按钮和标签)的首选方式是通过python还是kivy语言?

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?

推荐答案

使用模板创建自定义标签:

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而不是使用固定值进行计算:

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

更新

另一种方法是使用#:set语法设置变量:

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)

这篇关于在Kivy中设置全局字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆