简体   繁体   中英

tkinter text entry validation

I'm trying to validate the entry of text using Python/tkInter

def validate_text():
    return False

text = Entry(textframe, validate="focusout", validatecommand=validate_text)

where validate_text is the function - I've tried always returning False and always returning True and there's no difference in the outcome..? Is there a set of arguments in the function that I need to include?

Edit - changed from NONE to focusout...still not working

I think the only thing your missing is an invalidcommand (or invcmd) . What are you expecting validatecommand (or vcmd) to do if it returns false? According to the Tk Manual (see below), if vcmd returns False and validate is not set to none then invcmd will be called. The typical command for invcmd is Tkinter.bell, which makes a ding sound. Also note that vcmd and invcmd are very touchy , and will turn validate to 'none' if they encounter an exception, if the widget is changed in anyway inside the vcmd or invcmd functions or if vcmd does not return a valid Tcl boolean. In particular textvariable is notorious for causing issues, and a section in Entry called valdation specifically deals with that.

Here are the relevant portions from Tk Command Entry (same for Spinbox). See below for more references.

Command-Line Name: -validatecommand or -vcmd
Database Name: validateCommand
Database Class: ValidateCommand
Specifies a script to eval when you want to validate the input into the entry widget. Setting it to {} disables this feature (the default). This command must return a valid Tcl boolean value. If it returns 0 (or the valid Tcl boolean equivalent) then it means you reject the new edition and it will not occur and the invalidCommand will be evaluated if it is set. If it returns 1, then the new edition occurs. See Validation below for more information.

Command-Line Name: -invalidcommand or -invcmd
Database Name: invalidCommand
Database Class: InvalidCommand
Specifies a script to eval when validateCommand returns 0. Setting it to {} disables this feature (the default). The best use of this option is to set it to bell. See Validation below for more information.

Have a look at this SO answer , the Tk commands and epydoc-Tkinter for more references.

There are so many duplicates to this question. Python tkInter Entry fun
Restricting the value in Tkinter Entry widget

You should register your validate function.

def validate_text():
    return False

textframe.register(validate_text)
text = Entry(textframe, validate="focusout", validatecommand=validate_text)

focusout means validatecommand will be only be invoked when you take the focus out from the entrywidget.

You could try 'key' for validating while typing.

Tcl manual:
Validation works by setting the validateCommand option to a script which will be evaluated according to the validate option as follows:

  • none
    Default. This means no validation will occur.

  • focus
    validateCommand will be called when the entry receives or loses focus.

  • focusin
    validateCommand will be called when the entry receives focus.

  • focusout
    validateCommand will be called when the entry loses focus.

  • key
    validateCommand will be called when the entry is edited.

  • all
    validateCommand will be called for all above conditions.

"Note that this option [validatecommand] is only used if the validate option is not NONE"

From http://effbot.org/tkinterbook/entry.htm#entry.Entry.config-method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM