fontchooser(3tk) | Tk Built-In Commands | fontchooser(3tk) |
fontchooser - control font selection dialog
tk fontchooser configure ?-option value -option value ...?
tk fontchooser show
tk fontchooser hide
The tk fontchooser command controls the Tk font selection dialog. It uses the native platform font selection dialog where available, or a dialog implemented in Tcl otherwise.
Unlike most of the other Tk dialog commands, tk fontchooser does not return an immediate result, as on some platforms (Mac OS X) the standard font dialog is modeless while on others (Windows) it is modal. To accommodate this difference, all user interaction with the dialog will be communicated to the caller via callbacks or virtual events.
The tk fontchooser command can have one of the following forms:
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 Mac OS X for instance, the standard font selection dialog immediately reflects all user choices to the caller.
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.
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.
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.
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
dialog, font, font selection, font chooser, font panel
Tk |