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"))
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 validation relies on the three options that you can use for any input widget such as Entry widget: validate : specifies which type of event will trigger the validation. validatecommand : checks if a data is valid. invalidcommand : executes when the data is invalid.
An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.
The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, including macOS, as well as on Windows systems.
There is no attribute "required" in Tkinter, you need to write a function to check whether the user entered data in the entry or not. Then use this function as the command of the "Next" button. Save this answer.
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.
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.
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}
}
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 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.
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)
Of course, being passed the values for %P
and the other special character sequences is what makes the validation function so powerful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With