I need to insert a piece of code in the text of a Label that involves variables inside kv lang as shown below:
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang.builder import Builder
from kivy.core.window import Window
Window.size=(200,200)
Builder.load_string("""
<Screen>
BoxLayout:
orientation:'vertical'
size:root.width,root.height
Label:
id:label
text:'Nothing'
Button:
id:button
text:'Insert'
on_release: root.insert_text()
""")
class Screen(Widget):
def insert_text(self):
self.ids.label.text='Something' if button.text=='Insert' else 'Nothing' #Label should say "Something" if Button's text says 'Insert'
class App(App):
def build(self):
return Screen()
if __name__=='__main__':
App().run()
When running this code I get the error: NameError: name 'button' is not defined
How can I avoid this error and have the entered code work within kv lang?
I already tried putting self.ids.button.text instead of button.text and despite not getting the error, it doesn't work inside the application either.
Edit: Also tried insert button.text as a raw string but kv lang just ignored it.