Search code examples
pythonkivykivy-languagekivymd

How to get text from a textfield inside another classin kivy


I'm stuck in this problem, I'm using a dialog so that the user can change his password but i cant access the textfield text from the DashboardGerente class...

main.py

class Content(BoxLayout):
    old_pass_tf = ObjectProperty()
    new_pass_tf = ObjectProperty()
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.app = MDApp.get_running_app()

    def show_passnew(self,widget):
        if widget.state == "normal":
            self.ids.new_pass.password = True
        else:
            self.ids.new_pass.password = False
    def show_passold(self,widget):
        if widget.state == "normal":
            self.ids.old_pass.password = True
        else:
            self.ids.old_pass.password = False

class DashboardGerente(Screen):
    dialog = None
    cont = Content()
    def Logout(self):
        wm = MDApp.get_running_app().root
        wm.current = "Login"
    def teste(self):
        print(Login.username)
    def show_alert_dialog(self):
        if not self.dialog:
            self.dialog = MDDialog(
                title = "Alterar palavra-passe",
                text = "Palavra-passe antiga",
                content_cls=Content(),
                type="custom",
                buttons=[
                    MDFlatButton(
                        text="Cancelar",
                        text_color=rgba(0,0,0,1),
                        on_release = self.cancelar
                    ),
                    MDFlatButton(
                        text="Alterar",
                        text_color = rgba(0,0,0,1),
                        on_release =  self.change_pass
                    ),
                ],
            )
        self.dialog.open()

    def cancelar(self,obj):
        self.dialog.dismiss()

    def change_pass(self, obj):
        #i want to access the textfield text here 
        pass

.kv file

<Content>:
    orientation: "vertical"
    spacing: "12dp"
    size_hint_y: None
    height: "120dp"
    id: content_dialog_change_pass
    MDTextField:
        id: old_pass
        hint_text: "Palavra-passe antiga"
        mode: "rectangle"
        required: False
        color: 1, 0, 1, 1
        line_color_focus: 0.5, 0, 1, 1
        icon_right: "shield-key"
        size_hint: .9, None
        password: True

    GridLayout:
        cols:2
        MDCheckbox:
            on_press: root.show_passold(self)
            size_hint: None, None
            height: 5
            width: 20

        MDLabel:
            text: "Mostrar Palavra-passe"
            font_size: "13px"
            color: 0,0,0,.4

    MDTextField:
        id: new_pass
        hint_text: "Palavra-passe nova"
        mode: "rectangle"
        required: False
        color: 1, 0, 1, 1
        line_color_focus: 0.5, 0, 1, 1
        icon_right: "shield-key"
        size_hint: .9, None
        password: True

    GridLayout:
        cols:2
        MDCheckbox:
            on_press: root.show_passnew(self)
            size_hint: None, None
            height: 10
            width: 20

        MDLabel:
            text: "Mostrar Palavra-passe"
            font_size: "13px"
            color: 0,0,0,.4

<DashboardGerente@FloatLayout>:
    name: "DashboardGerente"
    MDBottomNavigation:
        panel_color: get_color_from_hex("#c300ff")
        text_color: 0,0,0,1
        MDBottomNavigationItem: ## Perfil do utilizador
            name: 'Profile'
            text: 'Perfil'
            icon: 'account'
            badge_icon: "numeric-10"
            MDCard:
                border_radius: 20
                radius: [15]
                size_hint:None, None
                size: 300, 400
                pos_hint: {"center_x": 0.5, "center_y": 0.5}
                elevation: 10
                orientation: "vertical"
                AnchorLayout:
                    anchor_x: "left"
                    anchor_y: "top"
                    MDIconButton:
                        id:logout
                        text_color: 0,0,0,1
                        on_release: root.Logout(), root.teste()
                        icon: "account-arrow-left"

                GridLayout:
                    cols: 2
                    padding: 40,0,0,0
                    MDLabel:
                        text: "ID: "
                    MDLabel:
                        text: "5000"
                    MDLabel:
                        text: "Nome: "
                    MDLabel:
                        text: "nome"
                    MDLabel:
                        text: "Cargo: "
                    MDLabel:
                        text: "cargo"
                    MDLabel:
                        text: "Salário: "
                    MDLabel:
                        text: "1000"
                    MDLabel:
                        text: "Secção: "
                    MDLabel:
                        text: "Frutaria"
                MDRaisedButton:
                    text: "Mudar palavra-passe"
                    md_bg_color: 0.5, 0, 1, 1
                    on_press: root.teste()
                    pos_hint: {"center_x": 0.5, "center_y": 0.5}
                    on_release: root.show_alert_dialog()

        MDBottomNavigationItem:
            name: 'Providers'
            text: 'Fornecedores'
            icon: 'truck'
            MDLabel:
                text: 'Mail'
                halign: 'center'

        MDBottomNavigationItem:
            name: 'Products'
            text: 'Produtos'
            icon: 'shopping'
            badge_icon: "numeric-10"
            MDLabel:
                text: 'Mail'
                halign: 'center'

        MDBottomNavigationItem:
            name: 'Employes'
            text: 'Empregados'
            icon: 'account-multiple'
            badge_icon: "numeric-10"
            MDRaisedButton:
                text: "Adicionar funcionários"
                md_bg_color: 0.5, 0, 1, 1
                pos_hint: {"center_x": 0.5, "center_y": 0.5}

        MDBottomNavigationItem:
            name: 'Sections'
            text: 'Secções'
            icon: 'storefront'
            badge_icon: "numeric-10"
            MDLabel:
                text: 'Mail'
                halign: 'center'

This are my project files that matter for now, how can i get the .text value from the old_pass textfield in the DashboardGerente class?


Solution

  • I would suggest as follows,

    Create a property, say, user_input = StringProperty("") in your App's subclass. then in kv lang of Content class,

    MDTextField:
        id: old_pass
        on_text: app.user_input = self.text
    

    Now use this in any method of DashboardGerente as MDApp.get_running_app().user_input