我希望能够访问在其他类中分配的变量值。这可能只与Kivy中的方法有关,但它通常与我对课堂交流的困惑有关。我的感觉是我需要将实例发送到树中,例如在this example中,但我似乎无法使其正常工作。
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()