option(n)
π option(n) β the itcl::option glow-up guide π
okay bestie, so this is itcl::option, a command from the [incr Tcl] world. its whole job? defining options for an extendedclass, a widget, or a widgetadaptor. that's it, that's the gig. π
also, full transparency: parts of this description are "borrowed" from the Tcl extension [snit], because the functionality is mostly identical. we're standing on shoulders, no shame. π«Ά
π¨ WARNING! π¨
This is new functionality in [incr Tcl] where the API can still change!!
like... it works, but the API is still in its situationship era. do not build your whole personality on it yet. π
β¨ SYNOPSIS β¨
option optionSpec ?defaultValue?
option optionSpec ?options?
πΈ DESCRIPTION πΈ
so the option command is what you use inside an [incr Tcl] extendedclass/widget/widgetadaptor definition to define options. it's the little declaration that says "hey, this thing has a knob, here's what it's called."
first form, first form, first form π’ β it defines an option for instances of this type, and optionally hands it an initial value. if you don't specify a defaultValue, the initial value defaults to the empty string. not "nothing", not null, the empty string. "". iconic.
an option defined this way is said to be locally defined. and the optionSpec? it's a list defining the option's name, resource name, and class name, like so:
option {-font font Font} {Courier 12}
now, the rules, and bestie these are rules:
- the option name must begin with a hyphen
-, and must not contain any upper case letters. lowercase only, we don't do shouting in option names. - the resource name and class name are optional. if you don't specify them, the resource name defaults to the option name minus the hyphen, and the class name defaults to the resource name with the first letter capitalized.
so yeah β this statement is equivalent to the previous example:
option -font {Courier 12}
same thing, less typing, we love efficiency. π
for more tea about resource and class names, go see The Tk Option Database.
options are normally set and retrieved using the standard instance methods configure and cget. and inside instance code (method bodies, etc.), option values are available through the options array:
set myfont $itcl_options(-font)
look at that. chef's kiss. ππ
second form β this is where you can define option handlers (like, say, -configuremethod). and ngl, if you're doing that, it should probably use configure and cget to access its options, to avoid subtle errors. subtle errors are the villain of this whole story, avoid them.
the option statement may include the following options:
-defaultdefvalue β defines the option's default value. and if you don't? the option's default value will be"". otherwise. yeah.-readonlyβ the option is handled read-only. it can only be set usingconfigureat creation time, i.e., in the type's constructor. after that? hands off, babe. π-cgetmethodmethodName β every locally-defined option may define a-cgetmethod; it gets called when the option's value is retrieved using thecgetmethod. whatever the method's body returns will be the return value of the call tocget.
the named method must take one argument: the option name. for example, this code is equivalent to (though slower than) Itcl's default handling of cget:
option -font -cgetmethod GetOption
method GetOption {option} {
return $itcl_options($option)
}
note that it's possible for any number of options to share a -cgetmethod. sharing is caring, fr fr.
-cgetmethodvarvarName β that is very similar to-cgetmethod, the only difference is, one can define a variable, where to find thecgetmethodduring runtime. runtime discovery, very main character. π-configuremethodmethodName β every locally-defined option may define a-configuremethod; it is called when the option's value is set using theconfigureorconfigurelistmethods. and here's the important bit: it is the named method's responsibility to save the option's value. in other words, the value will not be saved to theitcl_options()array unless the method saves it there. no cap. you gotta do it yourself.
the named method must take two arguments: the option name and its new value. for example, this code is equivalent to (though slower than) Itcl's default handling of configure:
option -font -configuremethod SetOption
method SetOption {option value} {
set itcl_options($option) $value
}
note that it's possible for any number of options to share a single -configuremethod.
-configuremethodvarvarName β that is very similar to-configuremethod, the only difference is, one can define a variable, where to find theconfiguremethodduring runtime.-validatemethodmethodName β every locally-defined option may define a-validatemethod; it is called when the option's value is set using theconfigureorconfigurelistmethods, just before the-configuremethod(if any). it is the named method's responsibility to validate the option's new value, and to throw an error if the value is invalid. gatekeeping, but make it functional. π§
the named method must take two arguments: the option name and its new value. for example, this code verifies that -flag's value is a valid Boolean value:
option -font -validatemethod CheckBoolean
method CheckBoolean {option value} {
if {![string is boolean -strict $value]} {
error "option $option must have a boolean value."
}
}
note that it's possible for any number of options to share a single -validatemethod.
-validatemethodvarvarName β that is very similar to-validatemethod, the only difference is, one can define a variable, where to find thevalidatemethodduring runtime.
π KEYWORDS π
option, widget, widgetadaptor, extendedclass
okay that's the whole page, you ate it, you understood it, you're literally a widget whisperer now. this gem lives in the itcl manual, version 4.0, under the name option(n). go forth and define some options, bestie πβ¨ππ¦
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- fedora 43: itcl 4.3.2-1.fc43 Β· TCL
- fedora 43: itcl 4.3.2-2.fc43 Β· TCL
- fedora 44: itcl 4.3.2-3.fc44 Β· TCL
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: incrtcl 4.2.3
- nixpkgs unstable: tcl 9.1.0
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 2008 Arnulf Wiedemann
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms