0

I want to be able to access the variable values assigned within other classes. This may be only related to the methods in Kivy but it is in general related to my confusion over the class communication. My feeling is I need to send the instances down the tree such as in this example but I can't seem to get it working.

from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.uix.textinput import TextInput

class LevelTwo(BoxLayout):
    def __init__(self, **kwargs):
        super(LevelTwo, self).__init__(**kwargs)
        self.wid = TextInput(id='var_id',text='string I want')
        self.layout = BoxLayout(id='box_layout_two')
        self.layout.add_widget(self.wid)

class LevelOne(BoxLayout):
    def __init__(self, **kwargs):
        super(LevelOne, self).__init__(**kwargs)
        self.layout_outer = LevelTwo(id='box_layout_one')

class RootWidget(BoxLayout):
    def __init__(self):
        super(RootWidget, self).__init__()
        self.widone = LevelOne()
        print(self.widone.box_layout_one.box_layout_two.var_id.text)
        --> stdout: '> string I want'

class MainApp(App):
    def build(self):
        return RootWidget()

if __name__ == '__main__':
    MainApp().run()
Griff
  • 2,064
  • 5
  • 31
  • 47
  • Neither `box_layout_one`, `box_layout_two`, nor `var_id` are variables - they are IDs that you have assigned to various components. Presumably Kivy has some way of looking up a component by its ID (I'm not a Kivy user), but that's not what you're doing here. Fortunately, you do have a chain of instance variables that lead to your TextInput: try `self.widone.layout_outer.wid.text`. – jasonharper May 15 '17 at 20:54
  • Indeed, I can access the text via what you provided. When I `print self.widone` I get `<__main__.LevelOne object at 0x103a06b40>` but when I do `print(self.widone.children)`, it is empty [], but I can still do `self.widone.layout_outer.wid...` etc. to access what I consider children variables. – Griff May 15 '17 at 21:10
  • `self.widone` indeed has no children - at no point did you add any children to it! It contains an instance variable that refers to an unrelated BoxLayout that does have one child, but that's not the same thing as a parent-child relationship. – jasonharper May 15 '17 at 21:29
  • Right, perhaps some folks more familiar with kivy can elaborate on how to communicate with `self.wid` variables at the `RootWidget`. – Griff May 15 '17 at 21:45

0 Answers0