Reputation: 81
Trying to work out how the bind with setter works in kivy. I find the documentation in the API really bad. They have a lot of expectation that there is tons of assumed knowledge around their kivy engine.
ANYWAY, rant over. Here is my code.
class ScreenOne(Screen):
story = StringProperty('')
def __init__ (self,**kwargs):
super (ScreenOne, self).__init__(**kwargs)
Buttonlayout = GridLayout(orientation='vertical',cols=1, spacing=2, size_hint_y=None)
Buttonlayout.bind(minimum_height=Buttonlayout.setter('height'))
btn = Button(text="change the text", size_hint_y=None, height=80,text_size=(350,None),font_size='12sp')
btn.bind(on_press=partial(self.changer,"text has changed"))
Buttonlayout.add_widget(btn)
root.add_widget(Buttonlayout)
self.add_widget(root)
def changer(self,*args):
self.story = args[0]
self.manager.current = 'screen2'
class ScreenTwo(Screen):
def __init__(self,**kwargs):
super (ScreenTwo,self).__init__(**kwargs)
my_box1 = BoxLayout(orientation='vertical')
L1 = Label(text=self.screenOne.story,font_size='24dp')
L1.bind(self.L1.setter('text')= story)
my_button1 = Button(text="Go to screen 1",size_hint_y=None, size_y=100)
my_button1.bind(on_press=self.changer)
my_box1.add_widget(self.L1)
my_box1.add_widget(my_button1)
self.add_widget(my_box1)
def changer(self,*args):
self.manager.current = 'screen1'
The idea is that when I press on the button in the class screenOne and it changes the screen it should change the label in screenTwo. I figured the best way to do this is with Properties and then to bind the label to the change in the StringProperty. I can change the string in the story fine, if I add a print instruction to it I can see that it changes. Its just the updating of the label I am having issue with.
I cant seem to get it to work. Help and direction would be greatly appreciated.
Upvotes: 1
Views: 545
Reputation: 5157
Note that the desired behavior can be (almost) entirely achieved with the kv
language only:
myapp.py
:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.app import App
class ScreenOne(Screen):
story = StringProperty('')
def changer(self,*args):
self.story += args[0]
self.manager.current = 'screen2'
class ScreenTwo(Screen):
pass
class MyApp(App):
pass
if __name__ == '__main__':
MyApp().run()
and my.kv
:
ScreenManager:
ScreenOne:
id: screen_one
name: 'screen1'
GridLayout:
orientation:'vertical'
cols:1
spacing:2
size_hint_y:None
on_minimum_height: self.setter('height')
Button:
text:"change the text"
size_hint_y:None
height: 80
text_size: (350,None)
font_size: '12sp'
on_press: screen_one.changer("text ")
ScreenTwo:
name: 'screen2'
BoxLayout:
orientation: 'vertical'
Label:
id: L1
text: screen_one.story
font_size: '24dp'
Button:
text: "Go to screen 1"
size_hint_y: None
size_y:100
on_press: root.current = 'screen1'
However, if you want to use the Label
's setter, change the MyApp
class definition:
class MyApp(App):
def build(self):
print self.root.ids
S1 = self.root.ids['screen_one']
L1 = self.root.ids['L1']
S1.bind(story=L1.setter('text'))
and make sure that the Label
's text
is static: text: 'start ...'
.
Upvotes: 2