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.


In reply to How Can My Perl/Tk Program use a Defined Font for All Widgets? by ozboomer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.