mikepol has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone,

I've had some trouble dealing with fonts in TK. What I want to do is query some widget for its current font, increase that font size and set the widgets font to this new font. I use $widget->configure('-font')->[4] to get the current font. But back I get the string "Courier -12". The question is: how do I interpet that string? I don't want to parse it myself because it can be something completely different. In fact, if I run my program with -font 24x12, I get the string "24x12". Is there a way that I can query a widget for its font and then safely parse that string into its family, size, etc.?

Replies are listed 'Best First'.
Re: increase font size in Perl TK
by zentara (Cardinal) on Aug 04, 2011 at 10:58 UTC
    It sounds pretty daunting to get it from a string of family and size and compare them to the string you get back from a widget about a font.

    One thing I have discovered recently about Tk fonts, is for the most part, you can forget passing the whole font-family string, Tk will happily just take the size. For instance, look at this extremely simple example.

    #! /usr/bin/perl -w use strict; use warnings; use Tk; my $size = 12; my $main = new MainWindow(); $main->geometry('400x400+200+200'); my $font = $main->fontCreate( -size => $size, -weight => 'bold'); my $button; $button = $main->Button(-text => "+", -font => $font, -command => sub{ $button->configure( -font=> $main->fontCreate( -size => $size++, -weight => 'bold')), } ); $button->pack(); MainLoop;
    and in case you havn't seen it yet, here is a font viewer for Tk, you probably can decipher all it's complexities, but the above method is probably simpler for controlling font size of a widget.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: increase font size in Perl TK
by cmv (Chaplain) on Apr 26, 2016 at 18:19 UTC
    Hi Monks-

    ++mikepol for asking
    ++zentara for his answer (and all his much appreciated pTk expertise over the years)

    I know this is an old thread, but just had a similar question and could not find a suitable answer anywhere (on-site or off-site).

    After banging my head against things for long enough, I came up with this solution. Hopefully, it will help others in the future.

    -Craig

    use strict; use warnings; use Tk; use Tk::Text; use Tk::Font; use Data::Dumper; # Create pTk text window... my $mw = MainWindow->new(-title => 'Text Font'); my $txt = $mw->Scrolled('Text')->pack(-fill => 'x', -side => 'left'); # Get Current font... my $currfont = $txt->cget(-font); # Create new font by cloning & modifying current font... my $boldfont= $currfont->Clone(-weight=>'bold'); # configure new tag ('bold') causing tagged characters to use the new +bold font... $txt->tagConfigure('bold', -font => $boldfont); # Insert some text that is not tagged as bold... $txt->insert('end', "========================\n"); $txt->insert('end', "Default font requested:\n" . Dumper($currfont)); $txt->insert('end', "\nDefault font chosen:\n" . Dumper($currfont->act +ual)); # Insert some text that is tagged as bold... $txt->insert('end', "========================\n"); $txt->insert('end', "Bold font requested:\n" . Dumper($boldfont), ['bo +ld']); $txt->insert('end', "\nBold font chosen:\n" . Dumper($boldfont->actual +), ['bold']); MainLoop;