option(3tk)

✨ option(3tk) β€” the Tk Built-In Commands era ✨

okay bestie, gather round, because we're talking about option from the Tk Built-In Commands collection β€” the little command that adds window options to the Tk option database and pulls them back out again. πŸŽ€


πŸ’– NAME πŸ’–

option β€” add and/or retrieve window options to and/or from the option database. that's it, that's the job. 🫢


🌸 SYNOPSIS 🌸

option add pattern value ?priority?
option clear
option get window name class
option readfile fileName ?priority?

four lil forms, four lil vibes. pick your fighter. πŸ’…


✨ DESCRIPTION ✨

so the option command lets you add entries to the Tk option database, or retrieve options from it. lowkey it's the group chat between you and the database. πŸ’¬

the add form β€” this one adds a new option to the database. pattern contains the option being specified, and it's made up of names and/or classes separated by asterisks or dots, in the usual X format (go peek at PATTERN FORMAT below, iykyk). value contains a text string to associate with pattern β€” this is the value that gets returned in calls to Tk_GetOption or by invocations of the option get command. if priority is specified, it tells you the priority level for this option (legal values are coming right up); it defaults to interactive. this command always returns an empty string. always. no notes. πŸ«₯

the option clear command β€” clears the option database. periodt. default options (from the RESOURCE_MANAGER property or the .Xdefaults file) get reloaded automatically the next time an option is added to the database or removed from it. and yes, like its sibling above, this command always returns an empty string. 😌

the option get command β€” returns the value of the option specified for window under name and class. now here's the drama, bestie: if several entries in the option database match window, name, and class, then the command returns whichever one was created with the highest priority level. and if there are several matching entries at the same priority level, it returns whichever entry was most recently entered into the option database. main character energy, most recent wins. πŸ‘‘ and if there are no matching entries at all? empty string. the void. πŸ’€

the readfile form β€” reads fileName, which should have the standard format for an X resource database such as .Xdefaults, and adds all the options specified in that file to the option database. if priority is specified, it indicates the priority level at which to enter the options; priority defaults to interactive. πŸ“‚βœ¨

πŸ¦‹ ok so important encoding tea: the file is read through a channel which is in "utf-8" encoding, and invalid byte sequences are automatically converted to valid ones. this means encodings like ISO 8859-1 or cp1252 with high probability will work as well, but this cannot be guaranteed. this cannot be changed, and setting the [encoding system] has no effect. it's giving "locked in, no edits". πŸ”’

πŸŽ€ the priority values πŸŽ€

the priority arguments to the option command are normally specified symbolically using one of these values, bestie:

  • widgetDefault β€” Level 20. used for default values hard-coded into widgets. the og defaults. 🧸
  • startupFile β€” Level 40. used for options specified in application-specific startup files.
  • userDefault β€” Level 60. used for options specified in user-specific defaults files, such as .Xdefaults, resource databases loaded into the X server, or user-specific startup files. it's giving personalization. πŸ’œ
  • interactive β€” Level 80. used for options specified interactively after the application starts running. if priority is not specified, it defaults to this level. the "I changed my mind live" level. πŸ™ˆ

any of the above keywords may be abbreviated. ⌨️ in addition, priorities may be specified numerically using integers between 0 and 100, inclusive. and ngl? the numeric form is probably a bad idea except for new priority levels other than the ones given above. like, it's not forbidden, it's just... risky behaviour. ⚠️


🌈 PATTERN FORMAT 🌈

patterns consist of a sequence of words separated by either periods, ., or asterisks *. the overall pattern may also be optionally preceded by an asterisk. yes, both darlings, they do different things. let's break it down. πŸŽ€

each word in the pattern conventionally starts with either an upper-case letter (in which case it denotes the class of either a widget or an option) or any other character, when it denotes the name of a widget or option. the last word in the pattern always indicates the option; the preceding ones constrain which widgets that option will be looked for in. πŸ•΅οΈβ™€οΈ

when two words are separated by a period, the latter widget must be a direct child of the former (or the option must apply to only the indicated widgets). strict parent-child energy, no skipping generations. πŸ‘¨πŸ‘©πŸ‘§

when two words are separated by an asterisk, any depth of widgets may lie between the former and latter widgets (and the option applies to all widgets that are children of the former widget). so basically "deep dive, any generation, we don't gatekeep". 🌊

if the overall pattern is preceded by an asterisk, then the overall pattern applies anywhere it can throughout the whole widget hierarchy. otherwise the first word of the pattern is matched against the name and class of the . toplevel β€” which are usually set by options to wish. πŸͺ„


πŸ’… EXAMPLES πŸ’…

example one β€” instruct every button in the application to have red text on it unless explicitly overridden, by setting the foreground for the Button class (note that on some platforms the option is ignored 😭):

option add *Button.foreground red startupFile

example two β€” allow users to control what happens in an entry widget when the Return key is pressed by specifying a script in the option database, and add a default option for that which rings the bell πŸ””:

entry .e
bind .e <Return> [option get .e returnCommand Command]
option add *.e.returnCommand bell widgetDefault

ate. no crumbs. 🍽️✨


πŸ”₯ SEE ALSO πŸ”₯


⭐ KEYWORDS ⭐

database, option, priority, retrieve


and that's the whole page, bestie!! 🫢 this one lives in the Tk manual, page option(3tk), hanging out under the Tk Built-In Commands banner (the page carries the Tk name and no dated version line to hand you, so it's just: Tk, option(3tk), forever iconic πŸ’–). go forth and populate that option database, you absolute queen. πŸ‘‘βœ¨

πŸ“¦ where this page comes from
  • ubuntu stonking: tk9.0-doc 9.0.4-1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms