Я пытаюсь получить доступ к TextInput.text одного класса (здесь «UserInput») из другого класса (здесь «GetInfoFromAnotherClass»). Однако кнопка «Получить информацию» дает только начальный ввод и не обновляется. Пока внутри класса "UserInput" это не проблема -> кнопка "Получить информацию". Что бы я ни вводил в текстовые поля, кнопка «Получить информацию» всегда возвращает «Первый ввод». Я больше не знаю, что искать в Google. Надеюсь, вы, ребята, можете мне помочь!
Вот пример моей проблемы «близкий к минимальному»:
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()
И 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()