Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables in Kv language (kivy)

Tags:

kivy

My Kivy Language file has many font_size properties, all with same values, is there away where I can assign a variable in KV lang? Current KV file sample:

#User ID
    Label:
        text: 'User ID'
        font_size: 20
        text_size: self.size

    TextInput:
        id: userid
        font_size: 20


    #User PW
    Label:
        text: 'Password'
        font_size: 20
        text_size: self.size

    TextInput:
        id: password
        password: True
        font_size: 20

    Button:
        text: 'Login'
        font_size: 20

Is is possible to set it somewhat like this:

#User ID
    @fs: 20
    Label:
        text: 'User ID'
        font_size: fs
        text_size: self.size

    TextInput:
    id: userid
    font_size: fs


#User PW
Label:
    text: 'Password'
    font_size: fs
    text_size: self.size

TextInput:
    id: password
    password: True
    font_size: fs

Button:
    text: 'Login'
    font_size: fs

By doing so, I would be able to change the font size at once only by changing the FS variable value, also, similar solution might help me to create theme based files faster. Thank you.

like image 631
surpavan Avatar asked Dec 12 '22 01:12

surpavan


2 Answers

I would be able to change the font size at once only by changing the FS variable value,

You can set a value with #:set name value, but this isn't quite what you want. Since you want the variable to update, you should use a kivy property so that the event system takes care of it for you.

In this case, since you want lots of different things to depend on size like that, you could for instance use a property of your app class.

class YourApp(App):
    font_size = NumericProperty(20)

then in kv

font_size: app.font_size

Any changes to the font_size of the App instance will then automatically propagate to these kv rules.

like image 106
inclement Avatar answered Dec 13 '22 14:12

inclement


Yes, there is a way. What you are looking for is this expression:

#:set name value

You can see the documentation here

Your .kv file:

#User ID
    #:fs 20
    Label:
        text: 'User ID'
        font_size: fs
        text_size: self.size

    TextInput:
    id: userid
    font_size: fs


#User PW
Label:
    text: 'Password'
    font_size: fs
    text_size: self.size

TextInput:
    id: password
    password: True
    font_size: fs

Button:
    text: 'Login'
    font_size: fs
like image 36
Mr. Baldan Avatar answered Dec 13 '22 14:12

Mr. Baldan