Reputation: 473
hello guys i am not able to get variable w value from this function but if it is out side of class so i can get the value but if it is in function i am not able to get value
my main.py
class ExampleApp(App):
def build(self,App):
self.load_kv("exapmleapp.kv")
def my_any():
w="THIS IS STRING"
if __name__ == "__main__":
ExampleApp().run()
This is my kv file
Label:
text:app.w
All i want is the label which has text stored in w variable
thanks in advance
This is error what i got
Traceback (most recent call last):
File "test.py", line 67, in <module>
ExampleApp().run()
File "/usr/local/lib/python2.7/dist-packages/kivy/app.py", line 797, in run
self.load_kv(filename=self.kv_file)
File "/usr/local/lib/python2.7/dist-packages/kivy/app.py", line 594, in load_kv
root = Builder.load_file(rfilename)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1749, in load_file
return self.load_string(data, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1828, in load_string
self._apply_rule(widget, parser.root, parser.root)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 2018, in _apply_rule
e), cause=tb)
kivy.lang.BuilderException: Parser: File "./exampleapp.kv", line 3:
...
1:
2:Label:
>> 3: text:app.w
...
BuilderException: Parser: File "./exampleapp.kv", line 3:
...
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 <module>
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)
Upvotes: 0
Views: 3375
Reputation: 1360
I think your question can be solved in several different ways.
What way to choose depends on where you want your function to exist.
Here is an answer that runs on my computer, with py3.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
mykv = Builder.load_string("""
<MyLabels>:
Label:
text: root.my_any()
Label:
text: '2'
""")
class MyLabels(BoxLayout):
def my_any(self):
print('in my_any')
w = 'this is a string'
return w
class ExampleApp(App):
def build(self):
return MyLabels()
if __name__ == '__main__':
ExampleApp().run()
I added an extra "root window" holding the widgets, that should make it a bit more easy to understand one way that also scales (the kv-file root now has < brackets > and the python get's an additional class to care for the root window).
Good to remember is that an App is an App, i.e. it's not part of the widget set, which may be more easy to attach functions and properties to. Another way to do it is with stringproperties (typically linked to a widget - like label/button), the documentation has some examples of that.
Upvotes: 2
Reputation: 183
The problem is that you are trying to get the value w
from your App class, which it don't have. w
only exists inside your method my_any
. Instead you would have to call my_any
and make it return your string value.
class ExampleApp(App):
def build(self,App):
self.load_kv("exapmleapp.kv")
def my_any():
w="THIS IS STRING"
return w # it has to return something for it to be accessible outside of the function
if __name__ == "__main__":
ExampleApp().run()
And then in the kv file you would have to change it to:
Label:
text:app.my_any()
So now your text will be equal to whatever your function returns.
Upvotes: 1