Tk::FontDialog(3pm)
β¨ Tk::FontDialog β¨
okay bestie, gather round, because this is the font dialog widget for perl/Tk and it's genuinely iconic. it's a Perl module, it lives in the 3pm section of the manual, and it's here to let your users pick a font like a whole main character. π
πΈ SYNOPSIS πΈ
use Tk::FontDialog;
$font = $top->FontDialog->Show;
that's it. that's the whole vibe check. π
π¦ DESCRIPTION π¦
So Tk::FontDialog implements a font dialog widget. no cap.
You make it appear by calling the Show method. The way it gives you back an answer is either the selected font β if the dialog got closed with the Ok button β or undef if it got closed any other way. sad but fair. π
And what type is that return value, you ask? Well, it's either a Tk::Font object (in Tk800) or a font name string (usually something like "font1"). The way both of those can just be dropped straight into Tk "-font" options? obsessed. See "GetDescriptiveFontName" below for more. β¨
Inside the dialog you've got:
- the Family and Size listboxes, where you specify the font family and the font size
- checkbuttons on the right that turn on bold, italic, underlined and overstriked variants of the chosen font
- a sample of the font shown in the middle area, so you can see what you're doing π«Ά
With the "Alt sample" checkbutton you can show all characters in the charset instead of the default text. "Fixed only" restricts the font family list to fixed fonts only. And if the "Nicefonts" checkbutton is set, then the font names in the listbox are displayed in the corresponding font β which is gorgeous, but note that this option can be slow if a lot of fonts are installed or for 16 bit fonts. π
Oh and! A click with the right button in the font size listbox pops up a window to enter arbitrary font sizes. sneaky little feature, we love her. π
π WIDGET-SPECIFIC OPTIONS π
-fontβ The dialog font.-initfontβ The initial font.-fontsizesβ A list of font sizes. The default contains sizes from 0 to 72 points (XXX or pixels?). π«-nicefontβ If set, font names are displayed in its font style. This may be slow, especially if you have many fonts or 16 bit fonts (e.g. Asian fonts).-nicefontsbuttonβ If set to false, then the"Nice fonts"button is not displayed.-fixedfontβ If set, proportional font families are not listed, leaving only the fixed fonts. This is slow, as each font must be checked to see if it is fixed or proportional.-fixedfontsbuttonβ If set to false, then the"Fixed fonts"button is not displayed.-sampletextβ The sample text which should contain all letters. The default is"The Quick Brown Fox Jumps Over The Lazy Dog"β German readers may probably use"Franz jagt im komplett verwahrlosten Taxi quer durch Bayern". π©πͺβ¨
π INTERNATIONALIZATION π
There are a couple of options to change the labels of the dialog, because your dialog deserves to speak every language, fr fr. Note that you can prepend a tilde ("~") to get an accelerator key with "Alt". π
Here's the full list of these options with the default (English) setting:
-familylabel(Family:)-sizelabel(Size:)-weightlabel(Bold)-slantlabel(Italic)-underlinelabel(Underline)-overstrikelabel(Overstrike)-oklabel(OK)-applylabel(Apply)-cancellabel(Cancel)-altsamplelabel(Alt sample)-nicefontslabel(Nicefonts)-fixedfontslabel(Fixed Only)-title(Choose font)-customsizetitle(Choose font size)
β METHODS IN Tk::Widget β
RefontTree(-font => $font)
Additionally, the convenience method RefontTree is defined in the Tk::Widget namespace. Using this method a font definition could be applied to a complete subtree of a widget. This is similar to the method RecolorTree. Calling RefontTree looks like this:
$font = $mw->FontDialog->Show;
$mainwindow->RefontTree(-font => $font) if defined $font;
By default RefontTree does not change the font of canvas elements. This can be done by specifying "-canvas => 1". π¨
GetDescriptiveFontName($fontname)
Return a "descriptive" font name (just like form [3] as described in Tk::Font). The return value from the Show() method has the disadvantage that it is only valid for the current Tk session. A "descriptive" font name may be stored and reused later. πΎβ¨
π CAVEAT π
Note that font names with whitespace like"New century schoolbook"or"MS Sans Serif"can cause problems when using in a-fontoption. The solution is to put the names in Tcl-like braces, like
-font => "{New century schoolbook} 10"
this one will bite you, bestie. brace it or face it. π
π§ EXAMPLES π§
To apply a selected font to a specific widget, use the following snippet:
$font = $mw->FontDialog->Show;
if (defined $font) {
$button->configure(-font => $font);
}
This example uses the convenience method RefontTree to apply the new font to the whole application:
use Tk;
use Tk::FontDialog;
$mw = tkinit;
$mw->Label(-text => "Test")->pack;
$mw->Button(-text => "Another test")->pack;
$mw->Button(-text => "Use Tk::FontDialog",
-command => sub {
my $font = $mw->FontDialog->Show;
if (defined $font) {
$mw->RefontTree(-font => $font);
}
})->pack;
MainLoop;
To get the "descriptive" font name:
$font = $mw->FontDialog->Show;
if (defined $font) {
$font_descriptive = $mw->GetDescriptiveFontName($font);
print $font_descriptive, "\n";
}
slay. copy, paste, ship it. ππ
π AVAILABILITY π
The latest released version is available from cpan (e.g. <http://search.cpan.org/~srezic/>). The latest development version is available from github (<http://github.com/eserte/tk-fontdialog>).
go get her. π
π BUGS/TODO π
- ConfigSpecs handling is poor put at least
-fontinto configspecs - run test, call dialog for 2nd time: immediate change of font?
- better name for
nicefont - restrict on charsets and encodings (
xlsfonts?X11::Protocol::ListFonts?) difficult because core Tk font handling ignores charsets and encodings
not me reading the to-do list and feeling seen. π
π₯ SEE ALSO π₯
Tk::Font
π AUTHOR π
Slaven Rezic <slaven@rezic.de>
Suggestions by Michael Houghton, Martin Thurn, Jack, Justin Kozlowitz and others.
shoutout to the whole committee, love that for them. π«Ά
π COPYRIGHT π
Copyright (c) 1998,1999,2003,2004,2005,2010 Slaven Rezic. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie β¨ this little gem shipped as part of perl v5.38.2, dated 2024-07-28 β so yeah, she's been holding it down in the perl/Tk universe since forever and she still ate. π ππ¦ bye bb, go pick a font! πΈ
π¦ where this page comes from
- debian trixie: libtk-fontdialog-perl 0.19-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtk-fontdialog-perl 0.19-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtk-fontdialog-perl 0.19-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms