问题描述
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
的情况下调用该函数?幕后到底发生了什么?
I have understood that the function check_the_input_only_allows_digits_only
is registered and for every character the user enters, the function is called and the input is validated. But why is the .register
required, couldn't the function be called without .register
every time the user enters something? What exactly is happening beneath the hood?
推荐答案
要知道的重要一点是,Tkinter 只是嵌入式 Tcl 解释器的薄包装.这意味着由于两种语言的根本差异,有时会有一些小的妥协.
The important thing to know is that Tkinter is just a thin wrapper around an embedded Tcl interpreter. That means there are sometimes small compromises due to fundamental differences in the two languages.
在 Tcl 中进行输入验证时,您指定一个 Tcl 脚本而不仅仅是一个可调用的函数.Tcl 将扫描代码中的特殊字符序列(例如 %P
、%S
等),并将它们替换为有关要验证的数据的信息.
When doing input validation in Tcl you specify a Tcl script rather than just a callable function. Tcl will scan the code for special character sequences (such as %P
, %S
, etc), and substitute them with information about the data to be validated.
用 Tcl 编写时,您的代码可能如下所示:
When written in Tcl your code might look something like this:
entry .amount -validate key -validatecommand {
expr {[string is int %P] || [string length %P]==0}
}
或者,使用 Tcl 函数:
Or, using a Tcl function:
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 的引用——通常是对函数或方法的引用.
The Python Way
Python doesn't have an easy way to pass around code as a string, and even if it did Tcl wouldn't understand it. Instead, in python you must pass a reference to a callable -- typically a reference to a function or method.
为了在 python 需要可调用的地方传递那些特殊的替换字符,您必须创建一个 Tcl 过程作为您的 python 函数的代理.该命令是在您调用 register
函数时创建的.
In order to pass in those special substitution characters where python expects a callable, you must create a Tcl procedure which acts as a proxy to your python function. This command is created when you call the register
function.
proc = creditor.register(check_the_input_only_allows_digits_only)
amount.config(validate = "key", validatecommand = (proc,"%P"))
如果不使用这些字符,则不需要注册命令.例如,以下代码是调用不带参数的函数的有效方法:
If you do not use these characters, you don't need to register the command. For example, the following code is a valid way to call a function that takes no parameters:
def check_the_input_only_allows_digits_only():
...
amount.config(validate = "key",validatecommand = check_the_input_only_allows_digits_only)
当然,传递 %P
和其他特殊字符序列的值是验证功能如此强大的原因.
Of course, being passed the values for %P
and the other special character sequences is what makes the validation function so powerful.
这篇关于为什么 tkinter 输入验证需要调用 register()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!