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 | |
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 | |
Re: How Can My Perl/Tk Program use a Defined Font for All Widgets?
by zentara (Cardinal) on Dec 09, 2018 at 15:55 UTC | |
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 |