Search code examples
kivykivymd

How to get the value from MDTextField inside a MDialog() in py file (kivymd2.0.1)


I want to get the value of text in the MDTextField. In .kv you just use the id as a reference like this:

id : my_text

but, in the MDialog() inside the py file you use = instead of : and a lot of values you just pass them as "" or between (). But with id, i dont know what to do. Look, this is the example:

from kivy.config import Config
Config.set('graphics', 'width', '375')
Config.set('graphics', 'height', '650')
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivymd.uix.button import MDButton, MDButtonText
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.textfield import (
    MDTextField,
    MDTextFieldHintText,
)
from kivymd.uix.dialog import (
    MDDialog,
    MDDialogButtonContainer,
    MDDialogContentContainer,
)

class MyBL1(BoxLayout):
    dialog = None
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def challenge_friend(self):
        if not self.dialog:
            self.dialog = MDDialog(
                MDDialogContentContainer(
                   MDTextField(
                        MDTextFieldHintText(
                            text= "Usuario",
                        ),
                    #id='tipo', #?????
                    mode= "outlined",
                   ),  
                pos_hint= {'center_x': 0.5},      
                orientation="vertical",
                spacing = "10dp",
                ),   
            MDDialogButtonContainer(
                Widget(),
                MDButton(
                    MDButtonText(text="Cancel"),
                    style="text",
                    #on_release = self.stop_challenge,
                ),
                MDButton(
                    MDButtonText(text="Aceptar"),
                    style="text",
                    #on_release = self.create_challenge()
                ),
            size_hint_x = 1,         
            spacing="10dp",
        ),
        )
        self.dialog.open()
    
    def create_challenge(self, *args):
        usuario = self.ids.tipo.text  #???
        print(usuario)   #?????
        self.dialog.dismiss()
        self.dialog = None

KV = """
<MyBL1>
    Button:
        font_size: "30sp"
        text: "Create Dialog"
        on_release: root.challenge_friend()
"""
Builder.load_string(KV)

class MyApp(MDApp):

    def build(self):
        self.screenm = ScreenManager()
        self.mybl_uno = MyBL1()
        screen = Screen(name="first screen")
        screen.add_widget(self.mybl_uno)
        self.screenm.add_widget(screen)
        return self.screenm


MyApp().run()

As you see, I tried putting the id reference as '' but, that does not work. By the way just id=tipo, doesn't work How I can acomplish this in this scenario? Thanks!


Solution

  • Another approach is to use the kv language and ids. Here is a modified version of your code that uses this approach:

    from kivy.config import Config
    from kivy.properties import ObjectProperty
    
    Config.set('graphics', 'width', '375')
    Config.set('graphics', 'height', '650')
    from kivymd.app import MDApp
    from kivy.lang import Builder
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.uix.boxlayout import BoxLayout
    from kivymd.uix.dialog import (
        MDDialog
    )
    
    # My extension of MDDialog
    class MyDialog(MDDialog):
        mybl1 = ObjectProperty(None)
    
    
    class MyBL1(BoxLayout):
        dialog = None
    
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
    
        def challenge_friend(self):
            if not self.dialog:
                self.dialog = MyDialog(mybl1=self)  # create MyDialog instead of MDDialog
            self.dialog.open()
    
        def create_challenge(self, *args):
            usuario = self.dialog.ids.tipo.text
            print(usuario)
            self.dialog.dismiss()
            self.dialog = None
    
    
    KV = """
    <MyBL1>
        Button:
            font_size: "30sp"
            text: "Create Dialog"
            on_release: root.challenge_friend()
            
    <MyDialog>:
        MDDialogContentContainer:
            pos_hint: {'center_x': 0.5}
            orientation: "vertical"
            spacing: "10dp"
            MDTextField:
                id: tipo
                mode: "outlined"
                MDTextFieldHintText:
                    text: "Usuario"
        MDDialogButtonContainer:
            size_hint_x: 1
            spacing: "10dp"
            Widget:
            MDButton:
                on_release: root.mybl1.stop_challenge()
                MDButtonText:
                    text: "Cancel"
                    style: "text"
            MDButton:
                on_release: root.mybl1.create_challenge()
                MDButtonText:
                    text: "Aceptar"
                    style: "text"
                
    """
    Builder.load_string(KV)
    
    
    class MyApp(MDApp):
    
        def build(self):
            self.screenm = ScreenManager()
            self.mybl_uno = MyBL1()
            screen = Screen(name="first screen")
            screen.add_widget(self.mybl_uno)
            self.screenm.add_widget(screen)
            return self.screenm
    
    
    MyApp().run()