将光标移动到MDTextField中字符串的最终位置

2024-09-26 17:55:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图给我正在用KivyMD开发的应用程序上的MDTextField提供一个日期格式。此类字段的格式应为“dd/mm/yyyy”

我想做的是,一旦写入前两个数字,就会自动写入一个“/”,光标会跳到“/”右边的最后一个位置(例如“21/”)。以同样的方式,在第一个“/”后面写入其他2个数字后,将写入第二个“/”,光标将再次移动到末尾(例如“21/09/”

我已设法使两个“/”都出现,但我无法将光标放在所需位置。我的代码如下:

def apply_date_format(self):
    # delete '/' if len is equal or less than 2 and final character is /  
    if len(self.ids.viajeInicio.text) =< 2 and (self.ids.viajeInicio.text).endswith('/'):
        self.ids.viajeInicio.text = (self.ids.viajeInicio.text[:-1])
    # first '/'
    elif len(self.ids.viajeInicio.text) == 2 and (self.ids.viajeInicio.text).isnumeric():
        self.ids.viajeInicio.text= self.ids.viajeInicio.text + "/"
    # second '/'
    elif len(self.ids.viajeInicio.text) == 5 and (self.ids.viajeInicio.text[3:5]).isnumeric():
        self.ids.viajeInicio.text= self.ids.viajeInicio.text + "/"
    # delete last '/' if len is <= 5 and last character is '/'
    elif len(self.ids.viajeInicio.text) > 3 and len(self.ids.viajeInicio.text) <= 5 \
             and (self.ids.viajeInicio.text).endswith('/'):
        self.ids.viajeInicio.text = (self.ids.viajeInicio.text[:-1])

MDTextField的id为viajeInicio,并且在on_text事件上调用函数apply_date_format。代码如下:

MDTextField:
    id: viajeInicio
    hint_text: 'Ingresar Fecha de Inicio del Viaje'
    pos_hint: {"x":0, "top":1}
    helper_text: 'Formato de fecha: dd/mm/aaaa'
    helper_text_mode: 'on_focus'
    required: True
    on_text:
        root.apply_date_format()

写入“/”后,如何将光标位置移动到字符串的末尾。此外,有没有更好的方法来完成预期的任务

提前多谢


Tags: andtextselfidsformatdatelenif
1条回答
网友
1楼 · 发布于 2024-09-26 17:55:43

我认为扩展MDTextField类并超越它的insert_text()方法更简单。大概是这样的:

class DateMDTextField(MDTextField):
    def insert_text(self, the_text, from_undo=False):
        if the_text == '/':
            # do not allow typed in '/'
            return
        cc, cr = self.cursor
        cur_text = self._lines[cr] + the_text  # existing text plus the to be inserted the_text
        cur_len = len(cur_text)

        # new_text will be inserted. The default is to just use the_text
        new_text = the_text

        # delete '/' if len is equal or less than 2 and final character is /
        if cur_len <= 2 and cur_text.endswith('/'):
            new_text = new_text[:-1]
        # first '/'
        elif cur_len == 2 and cur_text.isnumeric():
            new_text += '/'
        # second '/'
        elif cur_len == 5 and cur_text[3:5].isnumeric():
            new_text += '/'
        # delete last '/' if len is <= 5 and last character is '/'
        elif cur_len > 3 and cur_len <= 5 and cur_text.endswith('/'):
            new_text = new_text[:-1]
        # do not allow extra characters
        elif cur_len > 10:
            return

        # call the insert_text() of the MDTextField with the possibly modified text
        super(DateMDTextField, self).insert_text(new_text, from_undo=from_undo)

上面的代码使用了您的逻辑(添加了一些小的内容),由于它最终只调用了MDTextFieldinsert_text(),所以所有光标移动都是为您处理的

因此,您可以直接替换:

MDTextField:
    id: viajeInicio
    hint_text: 'Ingresar Fecha de Inicio del Viaje'
    pos_hint: {"x":0, "top":1}
    helper_text: 'Formato de fecha: dd/mm/aaaa'
    helper_text_mode: 'on_focus'
    required: True
    on_text:
        root.apply_date_format()

与:

DateMDTextField:
    id: viajeInicio
    hint_text: 'Ingresar Fecha de Inicio del Viaje'
    pos_hint: {"x":0, "top":1}
    helper_text: 'Formato de fecha: dd/mm/aaaa'
    helper_text_mode: 'on_focus'
    required: True

在你的“kv”里。您不需要显式调用insert_text()方法,因为它是由基类TextInput自动调用的。而且您也不需要on_text条目

您不再需要apply_date_format()方法

相关问题 更多 >

    热门问题