I am trying to get the Text from a variable which is located inside a function inside a different class. The variable gets its text value from an MDTextField from KivyMD. I've tried two approaches: getting the value directly with the MDTextField id and calling giving a variable the value of such text.
I believe the solution is really simple however I have not been able to do this correctly.
CODE FOR MINIMAL REPRODUCIBLE EXAMPLE
PYTHON FILE:
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.menu import MDDropdownMenu
from kivy.clock import Clock
class ActivityContent(BoxLayout):
menu = None
dropdown_value = ObjectProperty(None)
def dropdown_activity(self):
# Create the drop down menu
actividades = ["Correo electrónico", "Hora Regreso a Oficina", "Hora Salida de Oficina",
"Llamada de cortesia", "Llamada de Presentación", "Llamada de Promoción",
"Seguimiento a Cotización", "Traslado", "Visita a Cliente", "Whatsapp"]
menu_items = [{"text": f"{actividad}"} for actividad in actividades]
self.menu = MDDropdownMenu(
caller=self.ids.dropdown_Actividades,
items=menu_items,
width_mult=5,
)
self.menu.open()
self.menu.bind(on_release=self.set_item)
def set_item(self, instance_menu, instance_menu_item):
def set_item(interval):
self.ids.dropdown_Actividades.text = instance_menu_item.text
self.dropdown_value = self.ids.dropdown_Actividades.text
instance_menu.dismiss()
print(self.dropdown_value)
Clock.schedule_once(set_item, 0.5)
class AdditionalCommentsContent(BoxLayout):
def complete_registration(self):
print(ActivityContent.dropdown_value)
class IngActivWindow(Screen):
activity_container = ObjectProperty(None)
def activity_expansion_panel(self):
self.ids.activity_container.clear_widgets()
# Informacion de Actividad PANEL
self.ids.activity_container.add_widget(
MDExpansionPanel(icon="images/infoActividad.png", content=ActivityContent(),
panel_cls=MDExpansionPanelOneLine(
text="Información de Actividad")))
# Comentarios Adicionales PANEL
self.ids.activity_container.add_widget(
MDExpansionPanel(icon="images/comentarios.png", content=AdditionalCommentsContent(),
panel_cls=MDExpansionPanelOneLine(
text="Comentarios Adicionales")))
class MainMenuWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class ReproducibleExample(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return WindowManager()
if __name__ == "__main__":
ReproducibleExample().run()
KV FILE:
<WindowManager>:
id: screen_manager
MainMenuWindow:
id: main
name: 'main'
IngActivWindow:
id: ingActiv
name: 'ingActiv'
<MainMenuWindow>
MDRaisedButton:
text: 'Enter'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
on_press:
root.manager.current = 'ingActiv'
root.manager.transition.direction = 'right'
<IngActivWindow>:
name: 'ingActiv'
on_pre_enter: root.activity_expansion_panel()
FloatLayout:
cols:1
GridLayout:
id: activity_container
size_hint: 1, None
cols: 1
height: self.minimum_height
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
<ActivityContent>:
size_hint: 1, None
height: self.minimum_height
orientation: 'vertical'
padding: "15dp", "25dp", "15dp", "5dp"
# TIPO DE ACTIVIDAD
MDBoxLayout:
orientation: 'horizontal'
padding: "15dp", "5dp", "15dp", "5dp"
spacing: "5dp"
adaptive_height: True
size_hint: 1, None
MDTextField:
id: dropdown_Actividades
write_tab: False
size_hint: 1, None
pos_hint: {"x": 0, "center_y": 0.5}
icon_right: 'arrow-down-drop-circle-outline'
hint_text: 'Seleccionar Tipo de Actividad'
write_tab: False
enabled: False
on_focus: if self.focus: root.dropdown_activity()
MDBoxLayout:
size_hint_x: 0.1
<AdditionalCommentsContent>:
size_hint: 1, None
height: self.minimum_height
orientation: 'vertical'
adaptive_height: True
MDRaisedButton:
text: "Registrar Actividad"
size_hint: None, None
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
font_size: 12.5
on_release:
root.complete_registration()
The problem lies in the complete_registration function, inside the AdditionalCommentsContent class. I am trying to use the dropdown_value text. However instead of printing its actual value (like in the set_item function inside the ActivityContent class), it just prints <ObjectProperty name=dropdown_value>
. If I try with ActivityContent.dropdown_value.text
I get the following error:
AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'
As I mentioned before, I tried the self.ids.dropdown_Actividades.text
approach but this also didn't work.
How can I get the actual text value from this MDTextField?
Thanks a lot in advance.
question from:
https://stackoverflow.com/questions/65833561/get-text-of-variable-from-a-different-class