2

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?

Bryce
  • 197
  • 1
  • 1
  • 13
  • in the case of the link that you point out, you are modifying a property of the existing Label, the font_size. But a Widget does not have that property, and it does not have it because a Widget does not necessarily show a text, so it generates the error. Why do you establish a nonexistent property? Do you think magically kivy will create it? what's your objective? – eyllanesc Aug 14 '18 at 16:28
  • @eyllanesc, well, it works. At least in the example code I have provided it does change the font size. Which is part of my confusion. However, my main question stands even if you use Label. Maybe I'll edit it to just be using labels – Bryce Aug 14 '18 at 16:32
  • You could publish the code that generates the error, that's what we call an [mcve] – eyllanesc Aug 14 '18 at 16:34
  • @eyllanesc, a button does have a font_size property so I wasn't getting any errors using it in the kivy file but I was in python. So it was strange behavior. I also did publish the code that was generating that error. That wasn't my real question, it was just some extra information. I have edited the question now to remove the use of Widget – Bryce Aug 14 '18 at 16:47
  • in the case of kivy the creation of properties is dynamic, it is equivalent to writing about the constructor, so that is the solution. – eyllanesc Aug 14 '18 at 16:50

1 Answers1

3
  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

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • thanks! That was really obvious. I actually had tried that but using widget instead of Label. I was all turned around. Anyway, appreciate the help! – Bryce Aug 14 '18 at 17:33