如果未实现 invalid命令,如何使用 pattern 验证 customtkinter 条目?

How to validate customtkinter entry with pattern if invalidcommand is not implemented?

提问人:Jan_B 提问时间:11/8/2023 更新时间:11/8/2023 访问量:11

问:

出于 GUI 设计改进的原因,我目前正在努力将我的经典 tkinter 应用程序转换为 customtkinter。

虽然大多数原始应用程序的翻译都没有出现重大问题,但我现在面临着条目验证的问题。问题在于没有提供 .以下是我的旧逻辑的一些摘录,将 togeter 放在一个 reproduciple 示例中:customtkinterinvalidcommandCTkEntry

from tkinter import Tk, Entry, Button, StringVar
from re import compile


class App(Tk):
    def __init__(self):
        super().__init__()
        self.var = StringVar()
        # The entry to be validated:
        self.entry = Entry(self, textvariable=self.var, width=20, validate='focusout',
                           validatecommand=(self.register(validate_ap), '%P'),
                           invalidcommand=(self.register(self.reset_ap)))
        self.entry.pack()
        # The Button to start the processing of the entry information
        Button(self, text='process', command=self.print_if_valid).pack()

    # The function to change the entry if input is not allowed:
    def reset_ap(self):
        self.var.set('02200000')
        self.entry.after_idle(lambda: self.entry.config(validate='focusout'))

    # The function that handles the correct input further
    def print_if_valid(self):
        if self.var.get() != '02200000':
            print(self.var.get())


# The validation function:
def validate_ap(inp):
    pattern = compile(r'^(?!0+$)022\d{5}$')
    if pattern.match(inp):
        return True
    else:
        return False


if __name__ == '__main__':
    app = App()
    app.mainloop()

现在,如果我更改为如何在没有参数的情况下合并此行为?我喜欢把它放在里面,因为它向用户展示了所需的格式。此外,如果没有当前实现的验证,则不执行任何操作。customtkinterinvalidcommandEntry02200000Entryinvalidcommand

我正在考虑将逻辑实现到处理函数(这里是函数)中,但我的真正用例是一个具有此验证逻辑的多个输入的编辑器,因此我不想在用户完成整个表单后更改多个条目......invalidcommandprint_if_valid

此外,我想知道是否有更好的入住方式?我应该使用在验证中切换的布尔变量而不是我设置的占位符值,还是甚至有小部件的属性被 ?print_if_validEntryvalidatecommand

Python 验证 tkinter customtkinter

评论


答:

0赞 Jan_B 11/8/2023 #1

在写这个问题时,我发现了一个非常简单的解决方案,只需将 的逻辑合并到验证函数中:reset_apvalidate_ap

from tkinter import Tk, Entry, Button, StringVar
from re import compile


class App(Tk):
    def __init__(self):
        super().__init__()
        self.var = StringVar()
        # The entry to be validated:
        self.entry = Entry(self, textvariable=self.var, width=20, validate='focusout',
                           validatecommand=(self.register(self.validate_ap), '%P'))
        self.entry.pack()
        # The Button to start the processing of the entry information
        Button(self, text='process', command=self.print_if_valid).pack()

    # The function that handles the correct input further
    def print_if_valid(self):
        if self.var.get() != '02200000':
            print(self.var.get())

    # The validation function is now a classfunction and changes the entry directly instead
    # of returning True or False:
    def validate_ap(self, inp):
        pattern = compile(r'^(?!0+$)022\d{5}$')
        if not pattern.match(inp):
            self.var.set('02200000')
            self.entry.after_idle(lambda: self.entry.config(validate='focusout'))


if __name__ == '__main__':
    app = App()
    app.mainloop()

但是,我仍在考虑如何最好地检查该功能是否有效......self.varprint_if_valid