victorcd
victorcd

Reputation: 439

Action in another Screen with kivy

I am trying to make a app that I can execute some function in a screen, but the function acts in other screen. So, I have an app with screenmanager, this app has two screen, and each screen has two buttons. A button do something in other screen, and the other button change screen. But I can't execute any action upon other screen. I want to create a Numeric value, and I want this value may be show and manipulate, by any screen. My actual's code are: .py

class PrimeiroScreen(Screen):
    def __init__(self, **kwargs):
        self.name = 'um'
        super(Screen,self).__init__(**kwargs)

    def fc1(self):
        value = self.manager.get_screen('dois')
        self.ids.lb5.value += 5
        #self.ids.lb6.value += 5

I don't know how I can change this value in others screens, and how to show the same value on this two labels

class SegundoScreen(Screen):
    def __init__(self, **kwargs):
        self.name = 'dois'
        super(Screen,self).__init__(**kwargs)

    def fc1(self):
        self.ids.lb6.value += 5
        #self.ids.lb5.value += 5

and .kv

<PrimeiroScreen>:
    GridLayout:
        cols: 1     
        size_hint: (.2, .4)
        pos_hint:{'x': .35, 'y': .7}
        #padding: 30, 0
        spacing: 0, 10
        MyLabel:
            text:"Valor {}".format(self.value)
            font_size: '20dp'
            text_size: self.width, self.height
            id: lb5
        Button:
            text: "Voltar!"
            font_size: '20dp'
            text_size: self.width - 20, None
            size_hint: (.1,.1)
            pos_hint:{'x':.45, 'y':.05}
            on_release: root.manager.current = 'dois'
        Button:
            text: "Somar!"
            font_size: '20dp'
            text_size: self.width - 20, None
            size_hint: (.1,.1)
            pos_hint:{'x':.45, 'y':.3}
            on_press: root.fc1()

<SegundoScreen>:
    GridLayout:
        cols: 1
        size_hint: (.2, .4)
        pos_hint:{'x': .35, 'y': .7}
        #padding: 30, 0
        spacing: 0, 10
        MyLabel:
            text:"Valor{}".format(self.value)
            font_size: '20dp'
            text_size: self.width, self.height
            id: lb6
        Button:
            text: "Voltar!"
            font_size: '20dp'
            text_size: self.width - 20, None
            size_hint: (.1,.1)
            pos_hint:{'x':.45, 'y':.05}
            on_release: root.manager.current = 'um'
        Button:
            text: "Somar!"
            font_size: '20dp'
            text_size: self.width - 20, None
            size_hint: (.1,.1)
            pos_hint:{'x':.45, 'y':.3}
            on_press: root.fc1()

Upvotes: 1

Views: 592

Answers (1)

zeeMonkeez
zeeMonkeez

Reputation: 5157

You can get a specific Screen with ScreenManager's get_screen method. From there, you can access widgets through ids defined in kv:

screen2 = self.manager.get_screen('dois')
screen2.ids.lb6.text = 'New text'

Upvotes: 1

Related Questions