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

I've hit a situation where using the "-font" option in defining a Tk Text widget leads to a Segmentation Fault. The version below works correctly unless you switch the comment-out from line 22 to line 20. Does anyone know why?

#!/usr/bin/perl -w use strict; use Tk; use Tk::NoteBook; my $MW = MainWindow->new(); my $note = $MW->Label(-font=>'courier-12',-text=> "F1-4 presents/withdraws the notebook windows.")->pack(); my (@VW, @NB, %pg, %txt); foreach (0..3) { my $title = eval("$_+1"); $VW[$_] = $MW->Toplevel(-title=>"$title"); $VW[$_]->state('wit +hdrawn'); $NB[$_] = $VW[$_]->NoteBook()->pack(); foreach my $L ("A","B","C","D") { my $name = "$L-$title"; $pg{$name} = $NB[$_]->add($name,-label=>'empty'); } } foreach (keys %pg) {$txt{$_} = $pg{$_}->Text()->pack()} # this version causes a seg fault: #foreach (keys %pg) {$txt{$_} = $pg{$_}->Text(-font=>"courier-12")->pa +ck()} $MW->bind('all',"<Key-F1>"=>sub{toggle(0)}); $MW->bind('all',"<Key-F2>"=>sub{toggle(1)}); $MW->bind('all',"<Key-F3>"=>sub{toggle(2)}); $MW->bind('all',"<Key-F4>"=>sub{toggle(3)}); MainLoop;################## sub toggle { my $n = "@_"; my $state = $VW[$n]->state(); my $newstate = ($state eq 'withdrawn') ? 'normal' : 'withdrawn +'; $VW[$n]->state("$newstate"); }

Replies are listed 'Best First'.
Re: Tk Text -font=>segfault
by thundergnat (Deacon) on Mar 28, 2008 at 01:37 UTC

    It doesn't segfault on my system, but it uses an ugly system font; distinctly not courier. You need a space between the font name and the size.

    -font=>"courier -12"

    You can string together lots of modifiers, you just need to space separate them.

    -font=>"courier -12 bold italic"

    Did you want pixel sized fonts rather than point sized fonts? If the size argument is a positive number, it is interpreted as a size in points. If size is a negative number, its absolute value is interpreted as a size in pixels. If the font name has spaces in it You need to enclose it in curly brackets.

    -font=>"{bitstream vera sans} 12 bold italic"
Re: Tk Text -font=>segfault
by igelkott (Priest) on Mar 27, 2008 at 22:14 UTC

    Works for me with and w/o the font specification (Perl 5.10, ActiveState, WinXP).

    Could it be something as simple as needing to write "Courier" rather than "courier"? At least that's the way I'm used to seeing the font families written.

    Update: I guess the font family names are supposed to be case-insensitive so that shouldn't be the problem. Shouldn't even get a seg fault if Courier was for some odd reason missing from your system.

      (nb. this is not a missing or misnamed font issue)

      Oh no! Before i convince myself this is a 5.8.8 glitch and that i should upgrade, can someone with 5.10 on linux confirm igelkott's observation?

        This may or may not help you much, as I don't have Linux. But I've tried the code provided on my Mac running both 5.8.8 and 5.10, and neither seg faults. However, neither does what's expected, either. I'm guessing that if the user types in the text fields in the tabs, the text is supposed to render as 12 point Courier, and that doesn't seem to work for me (didn't on my Windows box running strawberry perl 5.10, either).

        However, there's a workaround. Add use Tk::Font then change

        foreach (keys %pg) {$txt{$_} = $pg{$_}->Text(-font=>"courier-12")->pack()}

        to

        foreach (keys %pg) {$txt{$_} = $pg{$_}->Text()->pack(); $pg{$_}->Font(family=>'courier',point=>12)}

        and see if that gets you what you need.

Re: Tk Text -font=>segfault
by jdporter (Paladin) on Mar 28, 2008 at 01:30 UTC
Re: Tk Text -font=>segfault
by zentara (Cardinal) on Mar 28, 2008 at 15:05 UTC
    Your script works fine for me on linux with Perl 5.8.8. I have seen this problem a few years ago, and it was solved by using fontCreate, instead of specifying a font point in each widget. Try this method of precreating the fonts:
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size=>int(-18*18/14)); $mw->fontCreate('medium', -family=>'courier', -weight=>'bold', -size=>int(-14*14/10)); my $entry = $mw->Entry(-font =>'big',-text=>'Big')->pack(); my $entry1 = $mw->Entry(-font =>'medium',-text=>'medium')->pack(); MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Tk Text -font=>segfault
by Anonymous Monk on Mar 28, 2008 at 09:07 UTC
    Doesn't segfault with v5.8.7 (ActivePerl Build 813), but no notebook shows either