Tk Source Code

View Ticket
Login
Ticket UUID: ed22529c92736860f8dafe0848ee26b0fb4095f9
Title: SpinBox validation is turned off when validate function returns false
Type: Bug Version: 8.6
Submitter: anonymous Created on: 2017-01-19 05:09:30
Subsystem: 17. [spinbox] Assigned To: fvogel
Priority: 6 Severity: Minor
Status: Closed Last Modified: 2017-01-23 20:12:10
Resolution: Fixed Closed By: fvogel
    Closed on: 2017-01-23 20:12:10
Description:
I am using python and tkinter but I don't think that is the source of the defect.
I add a validate function for my spinbox control and the normal range of the spinbox is zero to 5. In certain circumstances I want to reject values that are actually in that range, so I return false. If the user uses the spinbox arrows to change the value to (for example 3 when my validator thinks that is wrong) And my validate function returns FALSE, TK disables my validator function.
If I enter for example again a 3 *manually* (without the spinbox arrows) and my validator function returns FALSE, TK rejects the value properly.

I notice that the %V parameter for my validator is "forced" when using the spinbox arrows.

I have examined the TK source. "tkEntry.c:EntryValidateChange()" does not accept  a FALSE returned by a validator function when using "forced" and then turns off the validator function. A source code comment says "If we were doing forced validation (like via a variable trace) and the command returned 0, the we turn off validation"

I am not using variable tracing. I assume that the when using the arrow keys a SpinBox widget uses the "forced" validation type.
User Comments: fvogel added on 2017-01-23 20:12:10:
Merged to core-8-6-branch and trunk.

anonymous added on 2017-01-21 16:55:25:
That would have let me know to stop trying when I couldn't get it to work.
So, yes, that would be good.

fvogel added on 2017-01-21 13:08:20:

Would [2605e936eb6d7b00|this change] be the right one for everyone?


anonymous added on 2017-01-21 01:16:20:
option "a) Spinbox, -validate, -to, -from: not an appropriate combination as the spinbox's interface does not have a defined method to indicate and/or skip 
invalid values."

The validate function is still necessary with -to -from.
(Please note I'm only using python/tkinter. Perhaps I'm missing something)

A spinbox can *also* be edited. A user can type a value into the field. Not everyone wants to use the arrows. And in my testing the validate function works when the user types a value which is in the spinbox range but not desired by all of the other interacting spinboxes. My validate function worked when typing a value but not when using the spinbox arrows.

The spinbox arrows use "forced" and when the validate function is passed "forced" and it returns false the validate function is disabled. If I had read that anywhere it would have saved many hours of googling and experimenting and finally digging up the TK source code. "forced" wasn't documented anywhere I looked.
Again, I've got a work around by dynamically adjusting the ranges. I just wish I could have found the limitation faster.
Thanks again

bll (claiming to be [email protected]) added on 2017-01-20 10:53:06:
If you use the themed ttk::spinbox, the validator will not get disabled
when the -textvariable is changed.
If you use spinbox, the validator will need to be re-enabled.

There are notes in the manual pages about this situation and re-enabling validation.

bll (claiming to be [email protected]) added on 2017-01-20 10:38:46:
That's a very good question.   Now I see why the "forced" mode was added.

To summarize: 

a) Spinbox, -validate, -to, -from: not an appropriate combination as the spinbox's interface does not have a defined method to indicate and/or skip 
invalid values.

b) Spinbox, -validate, -values: This will work as long as the programmer
re-configures the -values option appropriately (and changes the current 
value of -textvariable if necessary).

So perhaps a documentation update?  Add a recommendation in the manual page to not mix -validate/-to/-from for spinbox and instead use -values if 
-validate is needed?

fvogel added on 2017-01-20 07:31:02:
Brad, what behavior would you expect from the spinbox when it is about reaching an invalid value? Skip the value? What if the next one is also invalid (despite still being in the list of -to/-from/-increment values)?

I don't think a spinbox with -to/-from/-increment specification of the range is the right widget to use in that situation. Instead of -to/-from/-increment, the programmer should use the -values option, and dynamically change its value if needed. Note that this is also complicated: say .spinbox1 has $value1, then .spinbox2 gets $value2 that makes value1 invalid, what would you expect to happen to $value1? This kind of behavior must be application-dependent, Tk cannot decide...

bll (claiming to be [email protected]) added on 2017-01-20 04:37:35:
I think I can understand the assumption that a spinbox's controls would return a valid value, and therefore the "forced" mode and not running the validation routine.

But I think that it is a "optimization" that really isn't needed, and in the OP's case, conflicts with their needs.  In most cases, using the spinbox's controls is going to produce a valid value, and the validator will return true anyways.

And the situation where after a "forced" return, the validator being completely deactivated seems like a definite bug.

My opinion, someone was trying to be too clever and introduced the "forced" mode when it really isn't necessary, and in the process created the bug where the validator gets deactivated.

One question is, is "forced" used in any of the other widgets, and do they have the same deactivation bug?  I'll look into that tomorrow if noone else does.

anonymous added on 2017-01-20 02:45:26:
Yes you have reproduced my defect. I am not using a text variable and I spent a long time carefully going over the documentation. This issue with "forced" was never mentioned and the comments in the code imply that the developer didn't consider the spinbox possibility. (okay. I *was* using read only text variables but I removed them on the possibility that they caused the problem)

My use case is that I have several spinboxes on a single dialog which interact.
Some interactions reduce the valid range. My work around is to update the range values for every spinbox in my dialog every time a number changes. That will work for me.
I can imagine more complicated interactions where particular elements (like the 3 in your test case) become dynamically available and not. I suppose you could programatically change the list ... if it were a short enough range. Perhaps someone might want to only reject a single number in a range of 30 or 40?

I can understand an argument that says a spinbox simply shouldn't do that ... If no change is made I think it would be valuable to update the documentation to say: if the type of validation is "forced" that means the validation function should not return "false" AND the spinbox documentation should mention that using the arrows is a "forced" data entry and not a "key" or something else.

Thank you very much for your detailed and prompt response!

fvogel added on 2017-01-19 22:00:28:

OK, after experimenting with the following script I see what you mean.

package require Tk

proc val {newval} {
  if {$newval == 3} {
    return false
  } else {
    return true
  }
}

spinbox .s -from 0 -to 5 -validate all -validatecommand "val %P" -textvariable myval
pack .s

proc showit {} {
  global myval
  puts "[.s cget -validate] - $myval"
  after 200 showit
}
showit

When using the spinbox arrows, the -validate option is reset to "none" when the validation command returns false (which in this example is when the user tries to move the spinbox value to "3").

Indeed your analysis in the source code is correct about how Tk resets the -validate option.

Now, what would you expect? Using a spinbox allows to select a value in a range of discrete values separated by an increment. The valid range of values are fully specified by the -to, -from and -increment options of the spinbox. If you use the validation command on top of this, and that validation command returns false (i.e. invalid) for one of the acceptable (i.e. valid) values as defined by -to, -from and -increment, then this is contradictory. Tk solves this contradiction by resetting the -validate option to "none" since it decided that the validate proc must be wrong because the acceptable values are given by -to, -from and -increment.

I don't see a bug in this behavior: the programmer instructed Tk with contradictory conditions about what are the valid values of the spinbox.

I believe your script should be corrected, not Tk. I'm questioning whether using a spinbox widget for the use you describe ("In certain circumstances I want to reject values that are actually in that range") is the right thing to do.


fvogel added on 2017-01-19 07:31:57:
On what Tk version, more precisely than "8.6"? There were bug fixes and documentation precisions on that very subject recently.

Are you perhaps writing -textvariable to something your validation proc does not accept? That could be the cause.

Validation is anyway a somewhat tricky thing, and I would advise you to read again the *latest* spinbox man page carefully. If you still think there is a bug then please provide a short snippet allowing to reproduce that bug.