7

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?

user
  • 5,370
  • 8
  • 47
  • 75
James_L
  • 1,225
  • 5
  • 19
  • 26

3 Answers3

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

qua-non
  • 4,152
  • 1
  • 21
  • 25
  • 8
    if you replace ` – MBarsi Sep 17 '14 at 16:39
  • 1
    Font sizes should be [expressed in sp, not dp](https://stackoverflow.com/questions/23981260/should-use-sp-instead-of-dp-for-text-sizes). Otherwise +1 for the helpful answer – Drake P Sep 22 '21 at 20:28
2

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)
Nykakin
  • 8,657
  • 2
  • 29
  • 42
  • Thank you! Interesting approach. Out of curiousity, is there also a way to do it using kivy's own label construct, without defining a new type of object? – James_L Sep 27 '13 at 18:30
  • @James_L, there is nothing wrong with declaring new types. Anyway, I updated this answer as well. – Nykakin Sep 27 '13 at 19:53
  • Thanks again. It's not that I have anything against any particular approach. I've only been using kivy for a little while; learning multiple ways of doing things helps me in getting to know the framework a bit better. – James_L Sep 27 '13 at 20:01
  • 3
    Nice answer, i just want to point out kv templates (the [MyWidget@Widget] synthax) are being deprecated and replaced by the more powerful dynamic classes ( instead, and replace "ctx" with "root")). – Tshirtman Sep 28 '13 at 10:04
-2

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
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36