Code Comments
Programming Forum and web based access to our favorite programming groups.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[d_] = $MW->Toplevel(-title=>"$title"); $VW[ _]->state
('withdrawn');
$NB[d_] = $VW[K_]->NoteBook()->pack();
foreach my $L ("A", B C D ) { my $name = "$L-$title";
MainLoop;###$pg{#name} = $NB[ _]->add($name,-labe =>'empty');
}
}
foreach (keys %pg) {$txt{$_} = $pg{a_}->Text()->pack()}
# this version causes a seg fault:
#foreach (keys %pg) {$txt{$_} = $pg{$_}->Text(-font=>
"courier-12")->pack()}
$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");
}
Post Follow-up to this messageperhaps because i am fooling with vim character encodings the code in the
post is a bit screwed up. Sorry! try:
#!/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
('withdrawn');
$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")->pack
()}
$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");
}
this is also on PerlMonks:
http://perlmonks.org/index.pl?node_id=676855
it may be a glitch in 5.8.8. If you use 5.10 on linux please test it for
me.
Post Follow-up to this messageOn Mar 27, 5:22 pm, MK <halfcountp...@intergate.com> wrote:
> 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?
> foreach (keys %pg) {$txt{$_} = $pg{a_}->Text()->pack()}
> # this version causes a seg fault:
> #foreach (keys %pg) {$txt{$_} = $pg{$_}->Text(-font=>
> "courier-12")->pack()}
I try to avoid using $_ as a loop variable. If something
changes $_ implicitly inside the loop it will corrupt the
hash. Note that there is only one $_.
Why not use something more straightforward:
while (($key, $w) = each %pg) {
$txt{$key} = $w->Text(-font=>"courier-12")->pack();
}
Post Follow-up to this messageMK wrote: > perhaps because i am fooling with vim character encodings the code in the > post is a bit screwed up. Sorry! try: te"); > } > > this is also on PerlMonks: > http://perlmonks.org/index.pl?node_id=676855 > > it may be a glitch in 5.8.8. If you use 5.10 on linux please test it for > me. Hi, can't reproduce your issue with 5.8.8 , latest perl/Tk. did you try -font => [courier => 12] or other font descriptions as well? Btw: Negative values for fontsizes are Pixels not Points. Christoph
Post Follow-up to this messageOn Thu, 27 Mar 2008 17:49:18 -0500, MK <halfcountplus@intergate.com>
wrote:
>perhaps because i am fooling with vim character encodings the code in the
>post is a bit screwed up. Sorry! try:
Your script works fine for me on linux, with Perl 5.8.8
In addition to what others have said, I have found from previous
problems like this, that it is best to declare your fonts first,
as shown below.
#!/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;
__END__
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.