Bryce
Bryce

Reputation: 207

Kivy - dynamically alter base widget property

I would like to change a base widget property in kivy. This is easily done in the kivy language like so:

<Label>:
    font_size: sp(30)

This does not have to be in my widget tree to affect all classes that use Label as a base class upon app initialization. So, my following example app shows up with two buttons whose text is larger than the default, which is what I want.

testApp.py

class Main_Screen(Screen):
    pass

class Root_Widget(ScreenManager):
    pass

class testApp(App):
    def build(self):
        return Root_Widget()

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

test.kv

<Root_Widget>:
    Main_Screen:
        BoxLayout:
            Label:
                text: 'Label1'
            Label:
                text: 'Label2'

<Label>:
    font_size: sp(30)

However, I would like to update this font_size value based on my window size (which I know how to change dynamically I just haven't included all that here). But how do I access the font_size property of the base class widget from python so that it can be changed?

Questions

How can I accomplish this change via python in a way that allows me to change it at the instantiation of my app?

Upvotes: 2

Views: 461

Answers (1)

ikolim
ikolim

Reputation: 16031

  1. Add import statement for label, from kivy.uix.label import Label
  2. Add import statement for sp, from kivy.metrics import sp
  3. Use Label.font_size = sp(30)

Snippet - Python code

from kivy.uix.label import Label
from kivy.metrics import sp

class testApp(App):
    def build(self):
        Label.font_size = sp(30)
        return Root_Widget()

Output

Img01

Upvotes: 3

Related Questions