Reputation: 1484
My application has a single button with text value as '2'. I want to change the text value to 100 on on_press
event
My attempt:
#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')
from random import random
from random import choice
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
r1c2=random()
Builder.load_string("""
<Highest>:
r1c2: str(2)
GridLayout:
cols: 1
Button:
text: root.r1c2
on_press: root.new()
""")
class Highest(Screen):
def new(self):
r1c2=str(100)
# Create the screen manager
sm = ScreenManager()
sm.add_widget(Highest(name='Highest'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
Error: Currently, nothing happens when the button is pressed. Please help
Upvotes: 1
Views: 1765
Reputation: 1152
You should be using kivy properties available. See kivy.properties for more information.
Add this import for access to string property:
from kivy.properties import StringProperty
And your Highest
class should be:
class Highest(Screen):
r1c2 = StringProperty(str(2))
def new(self):
self.r1c2 = str(100)
At initialization r1c2
value is equal to '2'. When the function new()
is called value of r1c2
will become '100'. Button text is bind to the string property r1c2
so it will automatically change.
You don't need r1c2=str(2)
in your builder string.
Builder.load_string("""
<Highest>:
GridLayout:
cols: 1
Button:
text: root.r1c2
on_press: root.new()
""")
Upvotes: 1