2015-07-12 22 views
4

我想從main.py文件中獲取一個名爲h的變量值到我的.kv文件,但是我得到的只是一個空白屏幕。從main.py傳遞變量值到.kv文件

這是我main.py

class ExampleApp(App): 

    def build(self): 
     h="SoME STRING IS HERE " 
     pass 

if __name__ == "__main__": 
    ExampleApp().run() 

這是我.kv文件

Label: 
    text:root.h.text 
+2

看到這裏,如果你有任何錯誤涉及與kivy文件連接。 http://stackoverflow.com/questions/30430780/python-kivy-kv-file-wont-read/30534840#30534840 – kiok46

+0

我試了兩個......但沒有工作...... – Nisarg

+0

你在構建方法中返回什麼? – kiok46

回答

3

這是你怎麼做。

Python文件

class ExampleApp(App): 
    h="SoME STRING IS HERE " 
    def build(self): 
     self.load_kv('t3.kv') 

if __name__ == "__main__": 
    ExampleApp().run() 

kivy文件

Label: 
    text: app.h #Here's the main difference 
+0

它工作@ kiok46 ...謝謝回答 – Nisarg

+0

很高興能幫到:) – kiok46

+0

但是如果h變量在某些函數中怎麼辦 – Nisarg