Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

kivy

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"
like image 895
Alex Ford Avatar asked Nov 29 '25 16:11

Alex Ford


1 Answers

Use ids and reference through AppScreenManager aka the ScreenManager.

self.parent.ids.screen2.screen2_label = new_label

See full example below.

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"
            print('Palim')
            #DemoScreen2.LabelUpdater(new_label)
            self.parent.ids.screen2.screen2_label = new_label
        else:
            new_label2 = "This is the new label when button 2 is pressed"
            self.parent.ids.screen2.screen2_label = new_label2
            #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() 

kv

AppScreenManager:
    DemoScreen1:
        id: screen1
    DemoScreen2:
        id: screen2

<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"
like image 106
PalimPalim Avatar answered Dec 02 '25 06:12

PalimPalim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!