Search code examples
pythonkivykivy-languagekivymd

Duplicate textfield widgets in the KiviMD dialog


Why when clicking change password button opens a dialog with duplicate textfield widgets? The code I wrote entirely in .py codes works fine but was not clear. That's why I transcribed it into .kv language.

enter image description here


Solution

  • demo2.py

    from kivy.lang import Builder
    from kivymd.app import MDApp
    from kivy.uix.screenmanager import Screen, ScreenManager
    from kivymd.uix.boxlayout import MDBoxLayout
    from kivymd.uix.dialog import MDDialog
    from kivymd.uix.button  import MDFillRoundFlatButton,MDIconButton ,MDFlatButton
    from kivymd.uix.textfield import MDTextField
    from kivy.uix.widget import Widget
    from kivy.properties import ObjectProperty
    
    
    PASSWORD = ["admin"]
    
    class Content(MDBoxLayout):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
        
    
    class UserScreen(Screen):
        pass
    
    class DataScreen(Screen):
        pass
    
    class Manager(ScreenManager):
    
        id = {}
    
        # def __init__(self, **kwargs):
        #     super().__init__(**kwargs)
            
        #     Manager.id["password"] = self.children[0].children[2]
        #     print(ChangePasswordScreen.x)
    
        def click(self):
            
            if Manager.id["password"].text == PASSWORD[0]:
                Manager.id["password"].text = ""
                self.current = "data_screen"
                self.transition.direction = "up"
            else:
                Manager.id["password"].text = ""
                print("špatné heslo")
        
        def show_change_password(self):
    
            print()
    
        
            
            self.dialog = MDDialog(
                title="Change password",
                type="custom",
                content_cls=Content(),  
                buttons=[
                    MDFlatButton(
                        text="CANCEL",
                        theme_text_color="Custom",
                        # text_color=DemoApp.styl.primary_color,
                    ),
                    MDFlatButton(
                        text="OK",
                        theme_text_color="Custom",
                        # text_color=DemoApp.styl.primary_color,
                    ),
                ],
            )
            
            self.dialog.open()
    
            
    
            # self.dialog.buttons[1].on_press = self.change_password
            self.dialog.buttons[0].on_press = self.dialog.dismiss
            
    
    class Demo2App(MDApp):
        styl = None
        def build(self):
            
            self.theme_cls.primary_palette = "Red"
            self.theme_cls.accent_palette = "Green"
            self.styl = self.theme_cls
            Builder.load_file("demo2.kv")
            
    Demo2App().run()
    

    demo2.kv

    <Content>:
        id: mywidget
        orientation:"vertical"
        spacing:"12dp"
        size_hint_y:None
        height:"200dp"
        MDTextField:
            hint_text:"current password "
            password:True
            pos_hint:{"center_x":0.5, "center_y":0.6}
            size_hint_x:None
            width:200
            focus:True
        MDTextField:
            hint_text:"New password"
            password:True
            pos_hint:{"center_x":0.5, "center_y":0.6}
            size_hint_x:None
            width:200
        MDTextField:
            hint_text:"Check new password "
            password:True
            pos_hint:{"center_x":0.5, "center_y":0.6}
            size_hint_x:None
            width:200
    
    
    Manager: 
        UserScreen:
            name: "user_screen"
            MDLabel:
                id:user_title
                text:"Přihlášení do databáze"
                font_style:"H4"
                pos_hint:{"center_x":0.5, "center_y":0.8}
                size_hint_x: None
                width: 500
                halign:"center"
                theme_text_color:"Secondary"
            MDTextField:
                id: password
                hint_text:"Password"
                password:True
                pos_hint:{"center_x":0.5, "center_y":0.6}
                size_hint_x:None
                width:200
                focus:True
            MDFlatButton:
                text:"Change password "
                pos_hint:{"center_x":0.5, "center_y":0.55}
                font_size:11
                on_press: root.show_change_password()
            MDFillRoundFlatButton:
                text:"Entry"
                pos_hint:{"center_x":0.5, "center_y":0.4}
                on_press:root.click()
        DataScreen:
            name: "data_screen"