fontchooser(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’… fontchooser β€” control font selection dialog

okay bestie, so this one lives in the Tk Built-In Commands section of the manual, and it's called fontchooser. that's the whole name. no notes. periodt.


✨ SYNOPSIS ✨

tk fontchooser configure ?-option value ...?
tk fontchooser show
tk fontchooser hide

like... three whole moods. configure, show, hide. that's the entire vibe. πŸ¦‹


🌸 DESCRIPTION 🌸

so the tk fontchooser command is literally in charge of the Tk font selection dialog. it uses the native platform font selection dialog where that's available, or a dialog implemented in Tcl otherwise. whichever one it can get, honestly. πŸ’–

now here's the thing that lowkey trips people up: unlike most of the other Tk dialog commands, tk fontchooser does not return an immediate result. why? because on some platforms (macOS) the standard font dialog is modeless, while on others (Windows) it's modal. to accommodate this difference, all user interaction with the dialog gets communicated to the caller via callbacks or virtual events. so like, don't sit there waiting for a return value, babe. it's giving asynchronous.

the tk fontchooser command can have one of the following forms:

  • tk fontchooser configure ?-option value ...? β€” Set or query one or more of the configuration options below (analogous to Tk widget configuration). yes, exactly like widget config. slay. πŸ’…
  • tk fontchooser show β€” Show the font selection dialog. depending on the platform, this may return immediately or only once the dialog has been withdrawn. so... could be instant, could be a whole journey. 🫢
  • tk fontchooser hide β€” Hide the font selection dialog if it's visible, and cause any pending tk fontchooser show command to return. bye bestie πŸ‘‹

πŸŽ€ CONFIGURATION OPTIONS πŸŽ€

  • -parent β€” Specifies/returns the logical parent window of the font selection dialog (similar to the -parent option to other dialogs). and here's the plot twist: the font selection dialog is hidden if it's visible when the parent window is destroyed. so like, parent goes down, dialog goes down too. ride or die. πŸ’€
  • -title β€” Specifies/returns the title of the dialog. Has no effect on platforms where the font selection dialog does not support titles. so on those platforms you're just out here naming a thing that has no name. tragic.
  • -font β€” Specifies/returns the font that's currently selected in the dialog if it's visible, or that will be initially selected when the dialog is shown (if supported by the platform). Can be set to the empty string to indicate that no font should be selected. Fonts can be specified in any form given by the "FONT DESCRIPTION" section in the font manual page. versatile queen ✨
  • -command β€” Specifies/returns the command prefix to be called when a font selection has been made by the user. The command prefix is evaluated at the global level after having the specification of the selected font appended. On platforms where the font selection dialog offers the user control of further font attributes (such as color), additional key/value pairs may be appended before evaluation. Can be set to the empty string to indicate that no callback should be invoked. Fonts are specified by a list of form [3] of the "FONT DESCRIPTION" section in the font manual page (i.e. a list of the form {family size style ?style ...?}). okay so form [3] specifically, not form [1] or [2]. iykyk. πŸ“
  • -visible β€” Read-only option that returns a boolean indicating whether the font selection dialog is currently visible. Attempting to set this option results in an error. it's read-only for a reason bestie, don't try it. πŸ™ˆ

πŸ¦‹ VIRTUAL EVENTS πŸ¦‹

  • <<TkFontchooserVisibility>> β€” Sent to the dialog parent whenever the visibility of the font selection dialog changes, both as a result of user action (e.g. disposing of the dialog via OK/Cancel button or close box) and of the tk fontchooser show/hide commands being called. Binding scripts can determine the current visibility of the dialog by querying the -visible configuration option. it's giving main character energy. πŸ‘‘
  • <<TkFontchooserFontChanged>> β€” Sent to the dialog parent whenever the font selection dialog is visible and the selected font changes, both as a result of user action and of the -font configuration option being set. Binding scripts can determine the currently selected font by querying the -font configuration option. font glow-up detected πŸ”₯

⭐ NOTES ⭐

Callers should not expect a result from tk fontchooser show and may not assume that the dialog has been withdrawn or closed when the command returns. All user interaction with the dialog is communicated to the caller via the -command callback and the <<TkFontchooser*>> virtual events. It is implementation dependent which exact user actions result in the callback being called resp. the virtual events being sent. Where an Apply or OK button is present in the dialog, that button will trigger the -command callback and <<TkFontchooserFontChanged>> virtual event. On some implementations other user actions may also have that effect; on macOS for instance, the standard font selection dialog immediately reflects all user choices to the caller. so like, mac is the oversharer of the group chat. 😭

In the presence of multiple widgets intended to be influenced by the font selection dialog, care needs to be taken to correctly handle focus changes: the font selected in the dialog should always match the current font of the widget with the focus, and the -command callback should only act on the widget with the focus. The recommended practice is to set font dialog -font and -command configuration options in per-widget <FocusIn> handlers (and if necessary to unset them β€” i.e. set to the empty string β€” in corresponding <FocusOut> handlers). This is particularly important for implementers of library code using the font selection dialog, to avoid conflicting with application code that may also want to use the dialog. like pls don't step on other people's toes in the group project. 🫢

Because the font selection dialog is application-global, in the presence of multiple interpreters calling tk fontchooser, only the -command callback set by the interpreter that most recently called tk fontchooser configure or tk fontchooser show will be invoked in response to user action and only the -parent set by that interpreter will receive <<TkFontchooser*>> virtual events. last one to call it wins. no cap. πŸ’€

The font dialog implementation may only store (and return) font actual data as the value of the -font configuration option. This can be an issue when -font is set to a named font: if that font is subsequently changed, the font dialog -font option needs to be set again to ensure its selected font matches the new value of the named font. so like, it doesn't auto-update. you gotta re-set it. it's not a mind reader bestie. πŸ™ˆ


🌈 EXAMPLE 🌈

okay here's the whole demo, and yes i'm giving it to you verbatim because it's iconic:

proc fontchooserDemo {} {
    wm title . "Font Chooser Demo"
    tk fontchooser configure -parent .
    button .b -command fontchooserToggle -takefocus 0
    fontchooserVisibility .b
    bind . <<TkFontchooserVisibility>> \
            [list fontchooserVisibility .b]
    foreach w {.t1 .t2} {
        text $w -width 20 -height 4 -borderwidth 1 -relief solid
        bind $w <FocusIn> [list fontchooserFocus $w]
        $w insert end "Text Widget $w"
    }
    .t1 configure -font {Courier 14}
    .t2 configure -font {Times 16}
    pack .b .t1 .t2; focus .t1
}
proc fontchooserToggle {} {
    tk fontchooser [expr {
            [tk fontchooser configure -visible] ?
            "hide" : "show"}]
}
proc fontchooserVisibility {w} {
    $w configure -text [expr {
            [tk fontchooser configure -visible] ?
            "Hide Font Dialog" : "Show Font Dialog"}]
}
proc fontchooserFocus {w} {
    tk fontchooser configure -font [$w cget -font] \
            -command [list fontchooserFontSelection $w]
}
proc fontchooserFontSelection {w font args} {
    $w configure -font [font actual $font]
}
fontchooserDemo

two text widgets, two fonts, one button, and a whole focus-handling situation. {Courier 14} and {Times 16} and everything. it ate. πŸ’…


πŸ‘‘ SEE ALSO πŸ‘‘


πŸ“ KEYWORDS πŸ“

dialog, font, font selection, font chooser, font panel


and that's the whole page bestie!! this one's from Tk, it's fontchooser(3tk), and it's the font selection dialog of your dreams. go forth and pick a font. slay. πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from