widget_validator(n)
π widget_validator β the widget::validator behaviour zone β¨
okay bestie so this is a page from the n manual, all about the widget::validator behaviour, and honestly? it's giving useful. let's get into it π
πΈ SYNOPSIS πΈ
package require Tcl 8.5
package require Tk 8.5
package require widget::validator ?0.1?
widget::validator attach w color cmdprefix
widget::validator detach w
widget::validator validate w
like, Tcl 8.5, Tk 8.5, and widget::validator version 0.1 (but the ?0.1? means it's flexible, iykyk). then three little methods β attach, detach, validate β that's the whole vibe π
β¨ DESCRIPTION β¨
so this package hands you a unified validation API for ttk's entry and combobox widgets. one API, both widgets, no chaos. we love to see it π«Ά
β οΈ okay but real talk: the validation behaviour in this package will not reject invalid edits. it will only highlight the entry that's holding invalid data and set the proper state flag. that's it. that's the whole job.
the actual deciding β how and when to actually reject that invalid content β is the responsibility of the using package or application. so like, the app does the rejecting, not us. periods. π
π
widget::validator attach w color cmdprefix
this method adds a validating behaviour to the widget w. no cap, it just... attaches. β¨
whenever the content of the widget's entry field changes, the specified cmdprefix gets invoked and it has to return a boolean value:
trueβ the content is ok πfalseβ the content is invalid π
for more info on the command prefix, go peek at the Validation section below. and in case of the latter β the invalid one β the background color of the entry field changes to color so everyone can see the invalidity. visual feedback slay π
π¨ the system does not support nesting validators on a widget, nor attaching multiple validators. none. zero. if you wanna change validating conditions, detach the current validator first, then attach the new one. order matters bestie.
an error is thrown if the widget already has validating behaviour attached to it. rude? maybe. correct? absolutely.
the result of the method is the empty string (that's just how it is).
to achieve its aims, the package overrides various configuration options of the widget the behaviour is attached to. those options are restored to their previous values on detach. very give-and-take of it π§
and a heads up, lowkey important: if other behaviours are attached, the validator may be rendered temporarily (partially) non-functional. similarly, if the validator is detached while a different behaviour is also attached, its restoration of configuration settings may render the other non-functional. so like... behaviours are sharing one closet and sometimes they fight over the same hanger π
π¦ widget::validator detach w
this one removes the validating behaviour from the widget w and restores it to its original state. clean break, no drama. π
an error is thrown if the widget has no validating behaviour attached to it. can't detach what was never attached, bestie.
the result of the method is the empty string.
β widget::validator validate w
invoking this method forces a validation of the widget w, assuming it has a validator behaviour attached to it. like poking it and going "hey, check now" π
the result of the method is the empty string.
π₯ VALIDATION π₯
the command prefix used for validation has to have this exact signature:
{*}cmdprefix text
the argument is the text to validate.
the result of the callback has to be a boolean value where:
trueβ text is ok β¨falseβ text is invalid π
that's the whole contract. no secret handshake. π
π EXAMPLE π
here's the whole thing in its full iconic glory, copy it exactly, don't touch a character:
package require Tk 8.5
package require widget::validator
set TE {}
set TC {}
ttk::entry .e -textvariable TE
ttk::combobox .c -textvariable TC -values {fruit vegetable corn}
ttk::combobox .n -values {fruit vegetable corn}
ttk::button .x -command ::exit -text Exit
pack .e -expand 1 -fill both -side top
pack .c -expand 1 -fill both -side top
pack .n -expand 1 -fill both -side top
pack .x -expand 1 -fill both -side top
widget::validator attach .e lightblue {apply {text {
expr {$text ne {}}
}}}
widget::validator attach .c yellow {apply {text {
expr {$text ni {{} hello world}}
}}}
widget::validator attach .n pink {apply {text {
expr {$text ni {{} blub}}
}}}
look at those little closures go π§ three widgets, three colours (lightblue, yellow, pink), three vibe checks. ate and left no crumbs π
π«Ά BUGS, IDEAS, FEEDBACK π«Ά
this document, and the package it describes, will undoubtedly contain bugs and other problems. like, we're being honest with you here. π
please report such in the category widget::validator of the Tklib Trackers at [http://core.tcl.cdk/tklib/reportlist] β wait, no, we're not doing typos here, the real one is http://core.tcl.tk/tklib/reportlist. go there. π
please also report any ideas for enhancements you may have for either the package and/or the documentation. we want the ideas, we want the glow-up β¨
π KEYWORDS π
invalid, state management, ttk::combobox, ttk::entry, validation, widget validation
okay that's the whole page, you're officially caught up, go validate something responsibly π¦
later bestie β this was tklib, version 0.1, and the page is widget_validator(n). mwah ππΈ
π¦ where this page comes from
- alpine 3.24: tk-lib-doc 0.7-r0 Β· TCL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms