2017-11-22 6 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을 사용해야합니다. 파이썬 파일 :

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 
여기

당신이 버튼을 누를 때 레이블 텍스트를 변경하는 방법에 대한 예입니다
관련 문제