python - Linking main.py with .kv file -
i trying label has text of string stored in w
variable in main.py
file , label in .kv file
let me show both files.
here's main.py file
from kivy.app import app kivy.properties import stringproperty class exampleapp(app): def hell(self): w="hello world" def build(self): self.load_kv("exapmleapp.kv") if __name__ == "__main__": exampleapp().run()
and here .kv
file.
label: text:app.w
every time when run main.py file, gives me error.
1: 2:label: 3: text:app.w ... attributeerror: 'exampleapp' object has no attribute 'w' file "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1649, in create_handler return eval(value, idmap) file "./exampleapp.kv", line 3, in text:app.w file "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 858, in getattribute return getattr(object.getattribute(self, '_obj'), name) file "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 2011, in _apply_rule value, rule, rctx['ids']) file "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1654, in create_handler cause=tb)
how can label has text stored in w variable in main.py file
i don't think .kv file can see attributes inside function calling. can see in example below, i've moved 'w' attribute outside function.
edit: aah seems want change displayed label when function hell() called. firstly need easy way refer label. kivy uses id tag, , list of ids can found in self.ids
having 'w' variable no longer necessary, i've left in there answer original question.
so, should work in theory:
from kivy.app import app kivy.properties import stringproperty class exampleapp(app): w = "hello" def hell(self): self.ids.mylabel.text = "world" def build(self): self.load_kv("exapmleapp.kv") if __name__ == "__main__": exampleapp().run()
and .kv file:
label: id: mylabel text:app.w
Comments
Post a Comment