gpt4 book ai didi

python-3.x - 为什么 tkinter 输入验证需要调用 register()?

转载 作者:行者123 更新时间:2023-12-01 12:07:22 24 4
gpt4 key购买 nike

def check_the_input_only_allows_digits_only(inp):
# A function for validating the input, the purpose of this is to let
# the user enter only digits.
if inp.isdigit() or inp is "" or inp == "\b" or inp is None:
return True
else:
return False


reg = creditor.register(check_the_input_only_allows_digits_only)
amount.config(validate = "key",validatecommand = (reg,"%P"))

我已经理解了函数 check_the_input_only_allows_digits_only注册并且对于用户输入的每个字符,都会调用该函数并验证输入。但为什么是 .register需要,如果没有 .register 就不能调用该函数每次用户输入什么?引擎盖下到底发生了什么?

最佳答案

要知道的重要一点是,Tkinter 只是一个围绕嵌入式 Tcl 解释器的薄包装。这意味着由于两种语言的根本差异,有时会有一些小的妥协。

Tcl方式

在 Tcl 中进行输入验证时,您指定一个 Tcl 脚本而不仅仅是一个可调用的函数。 Tcl 将扫描特殊字符序列的代码(例如 %P%S 等),并将它们替换为有关要验证的数据的信息。

用 Tcl 编写时,您的代码可能如下所示:

entry .amount -validate key -validatecommand {
expr {[string is int %P] || [string length %P]==0}
}

或者,使用 Tcl 函数:
proc check_the_input_only_allows_digits_only {P} {
expr {[string is int P] || [string length P] == 0}
}
entry .amount \
-validate key \
-validatecommand {check_the_input_only_allows_digits_only %P}

Python方式

Python 没有一种简单的方法可以将代码作为字符串传递,即使它这样做了 Tcl 也不会理解它。相反,在 python 中,您必须传递对 callable 的引用。 -- 通常是对函数或方法的引用。

为了在 python 需要可调用的地方传递那些特殊的替换字符,您必须创建一个 Tcl 过程作为您的 python 函数的代理。当您调用 register 时创建此命令功能。
proc = creditor.register(check_the_input_only_allows_digits_only)
amount.config(validate = "key", validatecommand = (proc,"%P"))

如果不使用这些字符,则不需要注册命令。例如,以下代码是调用不带参数的函数的有效方法:
def check_the_input_only_allows_digits_only():
...
amount.config(validate = "key",validatecommand = check_the_input_only_allows_digits_only)

当然,正在传递 %P 的值其他特殊字符序列使验证功能如此强大。

关于python-3.x - 为什么 tkinter 输入验证需要调用 register()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com