perlboy_emeritus has asked for the wisdom of the Perl Monks concerning the following question:
Greetings Monks, happy whatever each of you personally celebrate at this time of year.
The sample code, "fontviewer", I am including herein is not mine. I lifted it almost verbatim from "Mastering Perl/Tk", by Lidie and Walsh. The publisher offers a tar-ball for download of the book's source code. This example is from chapter 3. My mods, except for the unsuccessful hacking I did to get -underline and -overstrike to work are purely cosmetic. I chose font family Noteworthy solely to get a font that was instantly recognizable for what it is, rather than the generic font Tk chooses when it can't find the one you asked for. I call your attention to the sub:
&apply_font
therein is the problem. I have tried numerous hacks, all fail, to get -underline and -overstrike to work. They take True/False values and the associated CheckBoxes send either 0 or 1. Interestingly, the book code itself, what I have labeled "style 2" does not work. Only the un-commented out anonymous array works, marginally. Any attempts to add -underline and/or -overstrike expressions to that array cause failure in one form or another. Can anyone make those two font style options work? BTW: the widget demo that comes with Tk has an example where those two styles work with Courier. I am using Perl 5.36, Tk 804.036 in macOS 12.7.1. Thanks for any help.
#!/usr/bin/env -S perl ##!/usr/bin/env -S perl -d use Tk; use strict; use warnings; use v5.36; # not actually necessary, but it's what I run. require Tk::BrowseEntry; my $mw = MainWindow->new(-title => 'Font Viewer'); my $f = $mw->Frame->pack(-side => 'top'); my $family = 'Noteworthy'; my $be = $f->BrowseEntry( -label => "Family:", -variable => \$family, -browsecmd => \&apply_font, )->pack(-fill => 'x', -side => 'left'); $be->insert('end', sort $mw->fontFamilies); my $size = 20; my $bentry = $f->BrowseEntry( -label => 'Size', -variable => \$size, -browsecmd => \&apply_font, )->pack(-side => 'left'); $bentry->insert('end', (3 .. 32)); my $weight = "normal"; $f->Checkbutton( -onvalue => "bold", -offvalue => "normal", -text => "Weight", -variable => \$weight, -command => \&apply_font, )->pack(-side => 'left'); my $slant = "roman"; $f->Checkbutton( -onvalue => "italic", -offvalue => "roman", -text => "Slant", -variable => \$slant, -command => \&apply_font, )->pack(-side => 'left'); my $underline = 0; $f->Checkbutton( -text => "Underline", -variable => \$underline, -command => \&apply_font, )->pack(-side => 'left'); my $overstrike = 0; $f->Checkbutton( -text => "Overstrike", -variable => \$overstrike, -command => \&apply_font, )->pack(-side => 'left'); my $stext = "Sample Text, 1234567890, ABCDEF abcdef"; my $sample = $mw->Entry(-textvariable => \$stext)->pack(-fill => 'x'); $mw->Button(-text => 'Quit', -command => sub { $mw->destroy() }, -relief => 'raised', )->pack(-side => 'top', -ipadx => 5, -ipady => 5, -pady => 10); &apply_font; MainLoop; sub apply_font { $sample->configure(-font => [ $family, $size, $weight, $slant, ], # style 1 [ -family => $family, -size => $size, -weight => $weight, -s +lant => $slant, ], # style 2 [ -family => $family, # -size => $size, # -weight => $weight, # -slant => $slant, # -underline => $underline, # -overstrike => $overstrike, # ], ); warn "\$family: $family"; warn "\$underline: $underline"; warn "\$overstrike: $overstrike"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Tk -font options configuration failing
by choroba (Cardinal) on Dec 25, 2023 at 19:31 UTC | |
by Marshall (Canon) on Dec 26, 2023 at 07:32 UTC | |
by perlboy_emeritus (Scribe) on Dec 31, 2023 at 22:16 UTC |