Kvファイルで変数を使う

2015/10/24

Python 2.7.9, Kivy 1.9.0

Python側で設定したプロパティをKvファイル内で使うことが出来る。
ただし、プロパティにはNumericProperty、StringPropertyなどのクラスを使う。

# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.lang import Builder

from kivy.properties import NumericProperty

kv = '''
Button:
    text: 'Hello World'
    size: app.button_width, app.button_height
    size_hint: None, None
'''

class MyApp(App):
    button_width = NumericProperty(300)
    button_height = NumericProperty(150)

    def build(self):
        return Builder.load_string(kv)

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

Properties — Kivy 1.9.1-dev documentation
http://kivy.org/docs/api-kivy.properties.html

よく使いそうなの


Kivy - how to pass a variable to KV language using Clock
http://stackoverflow.com/questions/25241425/kivy-how-to-pass-a-variable-to-kv-language-using-clock

How to change text of a label in the kivy language with python
http://stackoverflow.com/questions/26656164/how-to-change-text-of-a-label-in-the-kivy-language-with-python