<MainWindow>: # root BoxLayout
<ISelect>: # Screen 1
my_list: my_list.__self__
BoxLayout:
BoxLayout:
id: my_list
Button:
Label:
<MDes>: # Screen 2
class ISelect(Screen):
def __init__(self, **kwargs):
super(ISelect, self).__init__(**kwargs)
self.index_button_3 = Button(text='Hello world 1')
self.my_list.add_widget(self.index_button_3)
<MainWindow>: # root BoxLayout
<ISelect>
: # Screen 1
my_list: my_list
BoxLayout:
BoxLayout:
id: my_list
Button:
Label:
<MDes>: # Screen 2
#With your other imports
from kivy.properties import ObjectProperty
class ISelect(Screen):
my_list = ObjectProperty() # Note that this is at the class level...
def __init__(self, **kwargs):
super(ISelect, self).__init__(**kwargs)
self.index_button_3 = Button(text='Hello world 1')
self.my_list.add_widget(self.index_button_3)
To keep the widget alive, a direct reference to the label_widget widget must be kept. This is achieved using id.__self__ or label_widget.__self__ in this case. The correct way to do this would be:
<MyWidget>: label_widget: label_widget.__self__
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
class IndexSelection(Screen):
topics_list = ObjectProperty()
# The function that will be called to add the button
def add_but(self):
self.topics_but_2 = Button(text='Hello world 1')
self.topics_list.add_widget(self.topics_but_2)
class CIMScreenManager(ScreenManager):
pass
class MainWindow(BoxLayout):
pass
class CIMgui(App):
def build(self):
return MainWindow()
def on_start(self, **kwargs):
super(CIMgui, self).on_start(**kwargs)
# Ensure that the correct screen is selected and call the add_but() function.
# I'm sure there's a better way of doing this...
self.root.ids['manager'].current = 'IndexSelectionScreen'
self.root.ids['manager'].current_screen.add_but()
CIMgui().run()