如何在 kivymd 文本字段上设置两种模式?

Posted

技术标签:

【中文标题】如何在 kivymd 文本字段上设置两种模式?【英文标题】:How can I put two mode on kivymd text field? 【发布时间】:2021-09-08 13:03:02 【问题描述】:

我正在制作一个登录屏幕,其中我们有两个文本字段(用户名和密码)

在用户名的文本字段中,我可以输入两个text field mode (on_error and persistent),就像我输入on_error一样,它仅在我单击文本字段时出现,所以我们不知道有错误。

请查看此视频,它包含我们的问题(不到一分钟).. https://youtu.be/sxx3-KqcNJQ

Design.kv

<LoginScreen>:
    GridLayout:
        cols: 1
        MDToolbar:
            title: "MainApp"

        RelativeLayout:
            # a card containing the fields and button for a login page
            MDCard:
                size_hint: 0.6, 0.6
                orientation: 'vertical'
                pos_hint: 'center_x': 0.5, 'center_y': 0.5
                elevation: 10

                RelativeLayout:
                    MDLabel:
                        text: "Sign in"
                        theme_text_color: "Secondary"
                        pos_hint: 'center_y': 0.88, 'center_x': 0.55
                        font_style: 'H4'

                    MDTextField:
                        id: username
                        hint_text: 'User name'
                        helper_text: ''
                        helper_text_mode: 'on_error'
                        icon_right: ''
                        size_hint: 0.8, .18
                        pos_hint: 'center_x': 0.5, 'center_y':  0.65

                    MDTextField:
                        id: password
                        hint_text: 'Password'
                        password: True
                        password_mask: '•'
                        color_active: app.theme_cls.primary_light
                        icon_left: "key-variant"
                        size_hint: 0.8, .20
                        pos_hint: 'center_x': 0.5, 'center_y':  0.4

                  
                    MDIconButton:
                        icon: "eye"
                        ripple_scale: .5
                        pos_hint: 'center_x': 0.85,"center_y": .4
                        pos: password.width - self.width + dp(8), 0
                        on_release:
                            self.icon = "eye" if self.icon == "eye-off" else "eye-off"
                            password.password = False if password.password is True else True

                    MDFlatButton:
                        text: 'Sign in'
                        font_size: dp(18)
                        pos_hint: 'center_x': 0.8, 'center_y': 0.15
                        text_color: app.theme_cls.primary_color
                        on_press: root.check_login()

Main.py

class LoginScreen(Screen):
    def check_login(self):

        login_csv = pd.read_csv('login.csv')
        i = 0

        for username in login_csv['username']:

            # if the username is empty the textfield turns red and shows the error message below
            if self.ids.username.text == '':
                self.ids.username.error = True
                self.ids.username.helper_text = 'This is a required field!'

            
            elif self.ids.username.text != username or self.ids.password.text != login_csv['password'][i]:

                # gives an error if the username or password is incorrect with the error message below
                self.ids.username.error = True
                self.ids.username.helper_text = 'Username or Password is incorrect!'
                self.ids.username.color_mode = 'custom'
                self.ids.username.line_color_focus = 1, 0, 0, 1
                self.ids.username.helper_text_color = 'error'
                i += 1

【问题讨论】:

【参考方案1】:

改变

helper_text_mode: 'on_error'

到`

helper_text_mode: 'on_error' and 'persistent'

它对我有用。

【讨论】:

以上是关于如何在 kivymd 文本字段上设置两种模式?的主要内容,如果未能解决你的问题,请参考以下文章

如何动态更改 MDDialog KivyMD 中的文本字段?

如何在kivymd focus agane中制作输入字段

如何在 kivymd 中添加多个屏幕?

如何从分配给 KivyMD Python 中的 MDLists 的 TextFields 获取数据

如何在 kivymd 按钮中对齐文本?

Kivymd:无法在 Scrollview 中添加多个文本字段和按钮