给KivyMD中的图标赋予功能

时间:2020-06-21 21:15:35

标签: python kivy icons textfield kivy-language

我想给一个在TextField中输入的图标一个功能,我想给它一个典型的函数,该函数具有密码的“ eye-off”图标,当您按下该图标时,将功能“ password”更改为False然后将图标更改为“ eye-on”,如果再次按下该按钮,则将其更改为True,然后将图标恢复为“ eye-off”,请允许我解释一下,这是我的主要代码:

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDRectangleFlatButton, MDIconButton
from kivy.lang import Builder
from helpers import *



class DemoApp(MDApp):

    def build(self):
        screen = Screen()
        self.theme_cls.primary_palette = 'Yellow'
        self.theme_cls.theme_style = 'Dark'

        btn = MDRectangleFlatButton(text='Confirmar', pos_hint= {'center_x': 0.5, 'center_y': 0.35},
                                    on_release= self.show_data)

        self.username = Builder.load_string(username_helper)
        self.contraseña = Builder.load_string(contraseña_helper)

        screen.add_widget(self.username)
        screen.add_widget(self.contraseña)
        screen.add_widget(btn)
        return screen

    def show_data(self, obj):
        print(f'Usuario: {self.username.text}\nContraseña: {self.contraseña.text}')

if __name__=='__main__':
    DemoApp().run()

在这里,我的文件处于字符串模式,这是我导入的帮助程序模块:

username_helper = '''
MDTextField:
    hint_text: 'Usuario'
    required: True
    helper_text: 'Enter text'
    helper_text_mode: 'on_error'
    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    size_hint_x: None
    width: 200
'''

contraseña_helper = '''
MDTextField:
    hint_text: 'Contraseña'
    password: True
    icon_right: "eye-off"
    pos_hint: {'center_x': 0.5, 'center_y':0.43}
    size_hint_x: None
    width: 200
'''

在这种配置下,输入密码时,一些字符串带有星号出现,但是我希望通过按右侧的“ eye-off”图标来看到正在输入的字符串,如果我再次按下它,再次用'*'隐藏,以此类推。

1 个答案:

答案 0 :(得分:0)

您可以通过扩展MDTextField来做到这一点:

class MyMDTextField(MDTextField):
    password_mode = BooleanProperty(True)

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            if self.icon_right:
                # icon position based on the KV code for MDTextField
                icon_x = (self.width + self.x) - (self._lbl_icon_right.texture_size[1]) - dp(8)
                icon_y = self.center[1] - self._lbl_icon_right.texture_size[1] / 2
                if self.mode == "rectangle":
                    icon_y -= dp(4)
                elif self.mode != 'fill':
                    icon_y += dp(8)

                # not a complete bounding box test, but should be sufficient
                if touch.pos[0] > icon_x and touch.pos[1] > icon_y:
                    if self.password_mode:
                        self.icon_right = 'eye'
                        self.password_mode = False
                        self.password = self.password_mode
                    else:
                        self.icon_right = 'eye-off'
                        self.password_mode = True
                        self.password = self.password_mode

                    # try to adjust cursor position
                    cursor = self.cursor
                    self.cursor = (0,0)
                    Clock.schedule_once(partial(self.set_cursor, cursor))
        return super(MyMDTextField, self).on_touch_down(touch)

    def set_cursor(self, pos, dt):
        self.cursor = pos

这将覆盖on_touch_down()的{​​{1}}方法,并检查图标附近是否发生触摸。如果是这样,它将切换图标和MDTextField的{​​{1}}设置。

您可以在password中以以下方式使用它:

MDTextField