Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How Can My Perl/Tk Program use a Defined Font for All Widgets?

by ozboomer (Friar)
on Dec 09, 2018 at 04:21 UTC ( [id://1226992]=perlquestion: print w/replies, xml ) Need Help??

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

Something of a silly question, I fear...

The code:

use strict; use warnings; use Tk; my $tstate = 0; # State of the dynamic text my $mw = MainWindow->new( # Create a window -width => 300, -height => 110, ); $mw->minsize( 300, 110 ); $mw->fontCreate('standard_font', # ..and some fonts +for it -family => 'Arial', -size => 12, -weight => 'normal'); $mw->fontCreate('alternate_font', -family => 'Times', -size => 24, -weight => 'bold'); $mw->fontCreate('my_default_font', -family => 'Sans', -size => 8, -weight => 'normal'); # $mw->configure(-font => 'my_default_font'); # '-font' is unkn +own to configure() # $mw->fontConfigure('my_default_font'); # Does nothing my $static_text_lbl = $mw->Label( # Uses the system ' +default' font -text => 'UNCHANGING TEXT', )->pack(-anchor => "center", -side => 'top'); my $text_lbl = $mw->Label( # An object with an + assigned font -text => 'DYNAMIC TEXT', -font => 'standard_font', )->pack(-anchor => "n", -side => 'top'); my $Exit_Btn = $mw->Button( # A button to exit -text => 'Exit', -width => 8, -command => sub { $mw->destroy }, )->pack(-anchor => 's', -side => 'bottom'); my $Toggle_Btn = $mw->Button( # ..and another to +toggle the font -text => 'Toggle', -command => [ \&fix_fonts ], )->pack(-anchor => 's', -side => 'bottom'); MainLoop; # ---------- sub fix_fonts { if ($tstate ^= 1) { $text_lbl->configure(-font => 'alternate_font') } else { $text_lbl->configure(-font => 'standard_font') } } # [eof]

I can assign a different font from the standard 'system defined' type through the command line invocation, viz:-

      c:\> perl prg.pl -font "sans 12"

...and the window and its child widgets will all have the "sans 12" font (unless it's explicitly changed).

However, it appears that if I want to do that same thing within the program itself, I have to do it explicitly on every widget, which seems redundant and annoying.

Is there a defined/accepted/working way to do things 'globally' within the program?

I've tried a couple of ways to apply a font to the Main Window (see in the code) and they don't seem to work.

I've tried checking on-line, in the Monastery, the O'Reilly books and some various examples of code... and I can't find anything that will do what the command line approach does.

This example is using ActiveState Perl v5.20.2 under Windows 8 32-bit.

I'd greatly appreciate any clues.

Thanks.

Replies are listed 'Best First'.
Re: How Can My Perl/Tk Program use a Defined Font for All Widgets?
by tybalt89 (Monsignor) on Dec 09, 2018 at 04:52 UTC

    It's

    $mw->optionAdd('*font' => 'sans 12');

    on my linux system.

Re: How Can My Perl/Tk Program use a Defined Font for All Widgets?
by johngg (Canon) on Dec 09, 2018 at 12:06 UTC

    I didn't know about the $mw->optionAdd('*font' => 'sans 12'); method, ++ tybalt89.

    What I have been doing is to use fontCreate() to create a font for the main window then add that font to the options hashes that I apply when creating widgets.

    my $mwFont = $mainWin->fontCreate( q{mwFont}, -family => q{courier}, -size => 10, ); $commonLabelOpts{ -font } = $mwFont; $commonButtonOpts{ -font } = $mwFont; $commonRadioButtonOpts{ -font } = $mwFont; ... $controlButtonFrame->Button( %commonButtonOpts, -text => q{Quit}, ...

    The optionAdd() method seems simpler so I will most likely use it for new code.

    Update: Changed example to correct the senior moment of choosing a widget without fonts ... what a dummy :-/

    Cheers,

    JohnGG

Re: How Can My Perl/Tk Program use a Defined Font for All Widgets?
by zentara (Archbishop) on Dec 09, 2018 at 15:55 UTC
    Hi, you can also try Tk::FontDialog and the RefontTree method.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::FontDialog; my $top=new MainWindow; my $font; my $fd; my $b = $top->Button(-text => 'Choose Font', -command => sub { $font = $fd->Show; apply_font($font); })->pack; my $f = $top->Frame->pack; $f->Label(-text => 'Test RefontTree 1')->pack; my $f2 = $f->Frame->pack; $f2->Label(-text => 'Test RefontTree 2')->pack; my $c = $f2->Canvas(-width => 100, -height => 30)->pack; $c->createText(0,0,-anchor => 'nw', -text => 'Canvas Text'); $fd = $top->FontDialog(-nicefont => 0, -title => 'Select Font', -applycmd => \&apply_font, -familylabel => 'Schrift~familie', -fixedfontsbutton => 1, -nicefontsbutton => 1, ); my $bf = $top->Frame->pack; $bf->Button(-text => 'OK', -command => sub { print "ok\n"; $top->destroy;})->pack(-side => 'left'); MainLoop; sub apply_font { my $font = shift; if (defined $font) { $b->configure(-font => $font); $f->RefontTree(-font => $font, -canvas => 1); } }

    I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: How Can My Perl/Tk Program use a Defined Font for All Widgets?
by ozboomer (Friar) on Dec 10, 2018 at 10:48 UTC

    Thanks for the clue, tybalt89. That lil' gem was the simplest of the lot.. and works well for my application.

    Basic, introductory info on these 'options' may be found in "16.2. Using the Option Database", a section in Mastering Perl/Tk... but I had to do a lot more searching 'n reading and try some things to make any sense of it. If you're familiar with "X" and *nix and how resources are defined, it will probably be helpful if you're trying to get your head around these sorts of questions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1226992]
Approved by marto
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-16 21:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found