use Tk; use Tk::Font; my $mw = MainWindow->new; my $l = $mw->Label; foreach (1 .. 10) { $l->Font } print "Fonts: " . join(",", $l->fontNames) . "\n"; #### use Tk; use Tk::Font; my $mw = MainWindow->new; my $font = $mw->Font(-size => 12); foreach (1 .. 10) { $mw->Label( -text => "Test", -font => $font )->pack; } $mw->Button( -text => "Enlarge Font", -command => sub { my $size = $font->configure('-size'); $font->configure(-size => ($size + 1)); } )->pack; MainLoop; #### use Tk; use Tk::Font; my $mw = MainWindow->new; print "Using Default Label Font:\n"; my $label = $mw->Label; printFontInfo($label); print "Using default Tk::Font:\n"; my $font = $label->Font; $label->configure(-font => $font); printFontInfo($label); print "Using custom Tk::Font\n"; $font = $label->Font(-family => 'Times'); $label->configure(-font => $font); printFontInfo($label); print "Using custom font:\n"; $label->configure(-font => "Arial"); printFontInfo($label); sub printFontInfo { my $w = shift; my $font = $w->cget('-font'); print "Name: $font\n"; print "Atrr: " . join(",", $font->actual) . "\n"; print "Fonts: " . join(",", $w->fontNames) . "\n\n"; } #### print "Size: " . $font->configure('-size') . "\n"; #### use Tk; use Tk::Font; my $mw = MainWindow->new; my $label = $mw->Label( -text => "Hello", -font => "Times 12" )->pack(-expand => 1, -fill => 'both'); $label->bind('', \&ResizeText); MainLoop; sub ResizeText { my ($widget) = @_; $widget->bind('', ""); ## Check to see if the font is "registered" ## so that we can use Tk::Font methods reliably my $knownFont = 0; my $font = $widget->cget('-font'); foreach my $f ($widget->fontNames()) { if ($$f eq $$font) { $knownFont = 1; last; } } ## If we're dealing with an unregistered font we ## could run into problems, so it's safest to create ## a new font based off the unregistered fonts attrs. ## Otherwise, reuse the existing font rather than ## recreating a new one. unless($knownFont) { $font = $widget->Font($font->actual); $widget->configure(-font => $font); } my $widthF = $font->measure($widget->cget('-text')); my $heightF = $font->metrics('-linespace'); my $xRatio = $widget->width / $widthF; my $yRatio = $widget->height / $heightF; my $minRatio = $xRatio < $yRatio ? $xRatio : $yRatio; my $fontSize = $font->actual('-size'); $font->configure(-size => ($minRatio * $fontSize)); $widget->update; $widget->bind('', \&ResizeText); }