James_L
James_L

Reputation: 1225

Setting global font size in 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?

Upvotes: 7

Views: 9917

Answers (3)

Yoav Glazner
Yoav Glazner

Reputation: 8066

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

Upvotes: -2

qua-non
qua-non

Reputation: 4162

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

Upvotes: 15

Nykakin
Nykakin

Reputation: 8747

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)

Upvotes: 2

Related Questions