widget_validator(3tk)
π widget_validator β bestie edition π
okay so this is widget_validator(3tk), a whole manual page from the widget::validator behaviour family, and honestly? she's giving "tiny package, huge responsibility." let's get into it π«Ά
β¨ NAME β¨
widget_validator β the whole widget::validator behaviour, that's her, that's the girl π
πΈ 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
π¦ DESCRIPTION π¦
so basically this package hands you a unified validation API for ttk's entry and combobox widgets. one API to rule them all, no cap β¨
β οΈ okay IMPORTANT bestie, read this twice: the validation behaviour defined in this package will not reject invalid edits. it will only highlight the entry containing invalid data and set the proper state flag. that's it. that's the whole job.
and like, it is the responsibility of the using package or application to decide how and when to actually reject such invalid content. so don't come for this package when your invalid text just sits there looking pretty in lightblue π
π the three methods π
widget::validator attach w color cmdprefixβ adds a validating behaviour to the widgetw. every time the content of the widget's entry field changes, the specifiedcmdprefixgets invoked and has to return a boolean value:truemeans the content is ok,falsemeans the content is invalid (more on the command prefix in the Validation section below, iykyk). if it comes back invalid, the background color of the entry field is changed tocolorto indicate the invalidity. π
the system does not support nesting of validators on a widget, nor the attachment of multiple validators. so if you wanna change validating conditions, detach the current validator first before attaching the new one. periodt.
an error is thrown if the widget has already had validating behaviour attached to it. π
the result of the method is the empty string.
to achieve its aims, the package overrides various configuration options of the widget the behaviour is attached to. these options are restored to their previous values on detach. good as new, no trace left behind π
β οΈ 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. it's giving chaos, so be careful out there.
widget::validator detach wβ removes the validating behaviour from the widgetwand restores it to its original state. glow-up reversed β¨
an error is thrown if the widget has no validating behaviour attached to it. (nothing to detach, bestie, there's nothing there π)
the result of the method is the empty string.
widget::validator validate wβ invoking this method forces a validation of the widgetw, assuming that it has a validator behaviour attached to it. like a vibe check but for your entry field π
the result of the method is the empty string.
β VALIDATION β
the command prefix used for validation has to have the following signature:
{*}cmdprefix text
- the argument is the
textto validate. - the result of the callback has to be a boolean value where
truemeans thattextis ok, andfalsethattextis invalid.
that's the whole contract, no cap π«Ά
π EXAMPLE π
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}}
}}}
three widgets, three colors, three little expr bouncers at the door. iconic behavior honestly π
π BUGS, IDEAS, FEEDBACK π
this document, and the package it describes, will undoubtedly contain bugs and other problems. (she said it herself, not me π) please report such in the category widget::validator of the Tklib Trackers at [http://core.tcl.tk/tklib/reportlist]. please also report any ideas for enhancements you may have for either package and/or documentation β they wanna hear from you, fr fr π
π KEYWORDS π
invalid, state management, ttk::combobox, ttk::entry, validation, widget validation
okay that's the whole page bestie, slay π
this was widget_validator(3tk), tklib version 0.1, and honestly she ate and left no crumbs. go forth and validate responsibly πΈπβ¨
π¦ where this page comes from
- debian trixie: tklib 0.9-1
- ubuntu questing: tklib 0.9-1
- ubuntu resolute: tklib 0.9-1build1
- ubuntu stonking: tklib 0.9-1build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms