I assume that from the BtnStack class in Directory.py, you want to access the ScreenManager class from the dynamically created buttons. There are a number of ways to do this.
Perhaps the most straight forward is to access the App instance and use that to access the ScreenManager. Here is a simplified example below. Notice that in the ButtonScreen.first_screen method, App.get_running_app is used to access the App instance, and from that app instance, the root widget can be accessed.
Hope this helps.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import Button
kv = """
<LabelScreen@Screen>:
BoxLayout:
orientation: 'vertical'
Label:
text: root.name
Button:
text: 'Next'
size_hint_y: None
height: dp(48)
on_release: root.manager.current = root.manager.next()
<ButtonScreen>:
GridLayout:
id: grid_layout
rows: 5
ScreenManager: # this is the root widget
LabelScreen:
name: 'One'
LabelScreen:
name: 'Two'
ButtonScreen:
name: 'Three'
"""
class ButtonScreen(Screen):
def on_kv_post(self, base_widget): # executes after kv is processed, so ids are valid
grid = self.ids.grid_layout # the grid id
for i in range(25):
button = Button(text=str(i), on_release=self.first_screen)
grid.add_widget(button)
def first_screen(self, _):
app = App.get_running_app() # the app instance
sm = app.root # the root widget (ScreenManager)
sm.current = 'One'
class DynamicButtonApp(App):
def build(self):
return Builder.load_string(kv)
DynamicButtonApp().run()