im new using classes and kivy/kivyMD, I have a trouble and it is that i want to get the text of an MDTextFied (Or Data "Login" of my Query) who belongs to the AScreen if im working from the class definition of the BScreen?
My minimal code is like, ScreenManager File:
<AScreen>:
name:'ascreen'
MDBoxLayout:
orientation: 'vertical'
MDTextField:
id: user
hint_text: 'User'
MDRectangleFlatButton:
text:'Login'
on_release: root.login_button()
<BScreen>:
name:'bscreen'
MDRectangleFlatButton:
text:'Registrar Equipo'
on_release: root.show_login_data()
The class file of my screens is:
class AScreen(Screen):
def login_button(self):
...
login = #User DB info
"""Here i do a query to the DB with the text of my MDTextField on AScreen
i use self.ids.user.text to get the text of the AScreen MDTextField
if the user exist in the DB i save the user's data on a variable named "login" """
class BScreen(Screen):
def show_login_data(self):
print()
For now I'm just trying to print the text from AScreen.ids.user.text or also get the login variable but I haven't got either of those.
My main file is:
sm = ScreenManager()
sm.add_widget(screensfile.AScreen(name='ascreen'))
sm.add_widget(screensfile.BScreen(name='bscreen'))
class MainApp(MDApp):
def build(self):
self.screen = Builder.load_string(screen_helper)
return self.screen
MainApp().run()
I have tried the following (I apologize for my horrible rookie mistakes): print(AScreen.login_button().login) print(Acreen.ids.user.text)
I know there are a similar cases here, but it doesn't help me for my screens.py file: Kivy - Use text from TextInput of one screen in another screen in .py file
get values from a different screen (kivy)
I know that the most probable thing is that I have a bad interpretation of the work with the classes, but with the way that the screens work in kivy (screens look for the definition of a class of the same name) I would not know how to do it. Could anyone help me?
Assuming that your screen_helper
defines a ScreenManager
as the Apps root widget with AScreen
and BScreen
as its children. In that case you can reference the MDTextField
in AScreen
as:
App.get_running_app().root.get_screen('ascreen').ids.user
The App.get_running_app()
returns the instance of the running App
. The root
references the root widget (which I am assuming is a ScreenManager
). The get_screen()
method of the ScreenManager
returns the instance of AScreen
, and the ids.user
gets a reference to the MDTextField
.
Trying to access the ids using the class AScreen
will not work as the ids are defined in the instance of AScreen
.
On an unrelated note, if your build()
method returns the results of Builder.load_string
, then the lines:
sm = ScreenManager()
sm.add_widget(screensfile.AScreen(name='ascreen'))
sm.add_widget(screensfile.BScreen(name='bscreen'))
are building an unused sm
, and can be removed.