2016-12-20 2 views
0

Kv 값 언어는 다음 구문을 사용하여 다른 파일에서 이름을 가져올 수있는 방법이 있습니다.py 파일의 변수를 .kv 파일로 가져 오는 방법은 무엇입니까?

#:import name x.y.z 

# The same as `from x.y import z as name` in Python code 

을하지만 그 KV 언어가 사용되는 동일한 모듈에서 값을 가져 오는 방법을 언급하지 않습니다.
의 나는 다음과 같은 코드가 있다고 가정 해 봅시다 :

from kivy.app import App 
from kivy.lang import Builder 
from kivy.atlas import Atlas 
from kivy.uix.button import Button 

theme = Atlas('path/to/atlas') 

Builder.load_string(''' 
<MyWidget>: 
    background_normal: theme['widget_bg'] 
''') 

class MyWidget(Button): 
    pass 

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

TestApp().run() 

내가 적절한 배경을 설정 KV 코드로 theme 아틀라스 객체를 가져올 것입니다. 어떻게이 일을 할 수 있겠습니까?

+0

로 현재 모듈을 참조 할 수 있습니다. –

+0

@GeorgeBou'루트'는 현재의 루트 위젯을 가리키며, 글로벌 네임 스페이스와 관련이 없다고 생각합니다. – Leva7

+0

위젯에서 전역 변수도 전달할 수 있습니다 ... 그게 효과가 없습니까? –

답변

0

당신은 당신이 변수를 사용할 필요가 어디서든 .kv 파일에`root.theme`를 사용해보십시오 __main__

#:import theme __main__.theme 

<MyWidget>: 
    background_normal: theme['widget_bg'] 
관련 문제