Reputation: 270
I have been trying to access the widgets in my .kv file from the python file and I have found this weird thing where if I access the IDs from the on_enter() or on_pre_enter() event from the screen, the .ids is empty and I can't access my widgets. In order to access them, I have to create another method and Clock.schedule it. Can someone explain me why this happens...
Working method
class MainScreen(Screen):
def add_labelinputs(self, dt):
print(self.ids)
def on_enter(self):
Clock.schedule_once(self.add_labelinputs)
Not working method (empty dict)
class MainScreen(Screen):
def on_enter(self):
print(self.ids)
Kivy file
#:kivy 1.11.1
WindowManager:
MainScreen:
<MainScreen>:
name: 'MainScreen'
GridLayout:
id: mainLayout
cols: 1
Upvotes: 1
Views: 80
Reputation: 39012
This is an issue with Kivy that has been around for a long time. It only affects the first Screen
added to a ScreenManager
in kv
. A workaround is to not use kv
for building the ScreenManager
. Try modifying your kv to:
#:kivy 1.11.1
<MainScreen>:
name: 'MainScreen'
GridLayout:
id: mainLayout
cols: 1
And modifying your build()
method to:
def build(self):
Builder.load_string(kv) # assumes kv string is defined as above
sm = WindowManager()
sm.add_widget(MainScreen())
return sm
Upvotes: 1