I am trying to access the TextInput.text of one class (here "UserInput") from another class (here "GetInfoFromAnotherClass"). However, the "Retrieve Info" Button only gives the initial Input and does not update. While from within the class "UserInput" it is no problem --> Button "Get Info". Whatever I put into the text fields, the "Retrieve Info"-Button always returns "First Input". I don't know what to google for anymore. Hope, you guys, can help me out!
Here is a "close to minimal" example of my problem:
import kivy
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
class Container(GridLayout):
pass
class UserInput(BoxLayout):
first_input = ObjectProperty(None)
second_input = ObjectProperty(None)
def __init__(self,**kwargs):
super().__init__(**kwargs)
def ui_btn(self):
print(self.first_input.text)
print(self.second_input.text)
class GetInfoFromAnotherClass(BoxLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.ui = UserInput()
def retrieve_info(self):
print(self.ui.first_input.text)
print(self.ui.second_input.text)
class MainApp(App):
def build(self):
return Container()
if __name__=='__main__':
MainApp().run()
And main.kv:
#:kivy 1.11.0
# Well this is just for Beauty ;-)
<MyTextInput@TextInput>:
size_hint_y: None
height: 50
multiline: False
write_tab: False
<MyButton@Button>:
size_hint_y: None
height: 50
<Container>:
cols: 1
UserInput
GetInfoFromAnotherClass
<UserInput>:
first_input: first_input
second_input: second_input
size_hint_y: None
height: self.minimum_height
padding: 20
MyTextInput:
id: first_input
text: "First Entry"
MyTextInput:
id: second_input
text: "Second Entry"
MyButton:
text: "Get Info in the same class"
on_press: root.ui_btn()
<GetInfoFromAnotherClass>:
size_hint_y: None
height: self.minimum_height
padding: 20
MyButton:
text: "Retrieve Info from another Class"
on_press: root.retrieve_info()
The self.ui = UserInput()
call creates a different UserInput
instance, one that nobody is using.
One way to access the text boxes is this:
UserInput
instance an id<Container>:
cols: 1
UserInput:
id: user_input_box
GetInfoFromAnotherClass:
class GetInfoFromAnotherClass(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.app = App.get_running_app()
# self.ui = UserInput()
def retrieve_info(self):
print(self.app.root.ids.user_input_box.ids.first_input.text)
print(self.app.root.ids.user_input_box.ids.second_input.text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.