2017-11-22 27 views
0

私は、IDを使用するための簡単な例を考え出すのに苦労しています。後で、ラベルやボタンのテキストを変更するなど、IDを使用してパラメータを変更する必要があります。だから私はどのように始めるのですか?私はIDを使用するための簡単な例を見つけることができません。Kivyの.kvファイルにidを追加するには?

import kivy 
from kivy.uix.gridlayout import GridLayout 
from kivy.app import App 

class TestApp(App): 
    pass 

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

.kvファイル:私はラベルのためのIDを使用してテキストを変更できるようにしたいと思います。この場合

#:kivy 1.0 

Button: 
    text: 'this button \n is the root' 
    color: .8, .9, 0, 1 
    font_size: 32 

    Label: 
     text: 'This is a label' 
     color: .9, 0, .5, .5 

答えて

0

kv言語hereに関する情報があります。 これにはkivy propertiesを使用する必要があります。ここ

は、ボタンを押したときに、ラベルのテキストを変更することができる方法の例です: Pythonのファイル:

import kivy 
from kivy.uix.gridlayout import GridLayout 
from kivy.app import App 
from kivy.properties import ObjectProperty 

class GridScreen(GridLayout): 
    label = ObjectProperty() #accessing the label in python 
    def btnpress(self): 
     self.label.text = 'btn pressed' #changing the label text when button is pressed 

class TestApp(App): 
    def build(self): 
     return GridScreen() 

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

KVファイル:

<GridScreen>: 
    label: label #referencing the label 
    rows: 2 
    Button: 
     text: 'this button \n is the root' 
     color: .8, .9, 0, 1 
     font_size: 32 
     on_press: root.btnpress() #calling the method 

    Label: 
     id: label 
     text: 'This is a label' 
     color: .9, 0, .5, .5 
関連する問題