How do you change a StringProperty value on a different screen?

1,289 views
Skip to first unread message

Alex Ford

unread,
Oct 13, 2017, 4:03:29 PM10/13/17
to Kivy users support

My app gets data from a database and and is stored into variables in Python. The code below is a simplified version where you have two screens. The first screen has two buttons and the second screen has a label and a back button. The text of the label on the second screen will change depending on what button is pressed.

When run, the label is set to the value of the StringProperty, which is "Test". When one of the buttons are clicked the ChangeScreen function is run and works out the correct new label. The LabelUpdater function on the second is run which should change the string property but doesn't. How do I fix this issue? Thanks <3


Python:
import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty

class DemoScreen1(Screen):
    def ChangeScreen(self, button_text):
        if button_text == "Button 1":
            new_label = "This is the new label when button 1 is pressed"
            DemoScreen2.LabelUpdater(new_label)
        else:
            new_label2 = "This is the new label when button 2 is pressed"
            DemoScreen2.LabelUpdater(new_label2)
        self.parent.current = "demoscreen2"

class DemoScreen2(Screen):
    screen2_label = StringProperty("Test")
    def LabelUpdater(NEW_LABEL):
        screen2_label = StringProperty(NEW_LABEL)

class AppScreenManager(ScreenManager):
    pass
class Tester(App): 
    pass
if __name__ == '__main__':
    Tester().run() 
Kivy:
AppScreenManager:
    DemoScreen1:
    DemoScreen2:

<DemoScreen1>:
    name: "demoscreen1"
    orientation: "vertical"
    GridLayout:
        rows: 2
        Button:
            id: Button1
            text: "Button 1"
            on_release: root.ChangeScreen(Button1.text)
        Button:
            id: Button2
            text: "Button 2"
            on_release: root.ChangeScreen(Button2.text)

<DemoScreen2>:
    name: "demoscreen2"
    orientation: "vertical"
    GridLayout:
        rows:2
        Label:
            text: root.screen2_label
        Button:
            text:"Back"
            on_release: app.root.current = "demoscreen1"

Andreas Ecker

unread,
Oct 14, 2017, 5:24:25 AM10/14/17
to kivy-...@googlegroups.com
Your `LabelUpdater()` method is missing the `self` parameter. Additionally you have to specify the `StringProperty` class name in the declaration - after that you can use your property `screen2_label` like a normal variable.

```
def LabelUpdater(self, NEW_LABEL):
        self.screen2_label = NEW_LABEL
```

There were some more quite basic python coding errors (e.g. the use of DemoScreen2 as a reference) in your code snippet- therefore recommending to first do a bit more of python learning/tutorials before you start programming with a quite complex framework like kivy. Anyway underneath you find a working version of your code snippet (put all into a new py file - no kv file needed):

```

import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
from kivy.lang import Builder

kv_text = '''\

AppScreenManager:
    DemoScreen1:
    DemoScreen2:
        id: demo_screen2


<DemoScreen1>:
    name: "demoscreen1"
    orientation: "vertical"
    GridLayout:
        rows: 2
        Button:
            id: Button1
            text: "Button 1"
            on_release: app.ChangeScreen(Button1.text)

        Button:
            id: Button2
            text: "Button 2"
            on_release: app.ChangeScreen(Button2.text)


<DemoScreen2>:
    name: "demoscreen2"
    orientation: "vertical"
    GridLayout:
        rows:2
        Label:
            text: root.screen2_label
        Button:
            text:"Back"
            on_release: app.root.current = "demoscreen1"
'''


class DemoScreen1(Screen):
    pass


class DemoScreen2(Screen):
    screen2_label = StringProperty("Test")
    def LabelUpdater(self, NEW_LABEL):
        self.screen2_label = NEW_LABEL



class AppScreenManager(ScreenManager):
    pass


class Tester(App):
    def build(self):
        return Builder.load_string(kv_text)


    def ChangeScreen(self, button_text):
        if button_text == "Button 1":
            new_label = "This is the new label when button 1 is pressed"
            self.root.ids.demo_screen2.LabelUpdater(new_label)

        else:
            new_label2 = "This is the new label when button 2 is pressed"
            self.root.ids.demo_screen2.LabelUpdater(new_label2)
        self.root.current = "demoscreen2"



if __name__ == '__main__':
    Tester().run()
```


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages