http://qs321.pair.com?node_id=1050326


in reply to Re^3: perl tk frame packing question
in thread perl tk frame packing question

In reply to your comment
"It's not enough because you don't call RefontTree again after destroing the HList. ItemStyle has a font option, make $font global and use it to set the font when the HList is recreated (-font => $font). "
While seleceting the fonts from the FontDialog, I was also printing the $font and its descripttive name. For example for 'courier 10 pinch' and font size 5 , the $font which we pass to -font is coming as font30.
I created a global variable at the top of my program as $font and assigned font30 to it. Then using that $font to RefontTree for $mw and $hlistframe. Even after destroying the hlistframes i am calling the RefontTree on $hlistframe and passing $font to -font. But it does not seem to be taking that font while the gui is launched.
Can you please show me a working example.

Replies are listed 'Best First'.
Re^5: perl tk frame packing question
by stefbv (Curate) on Aug 21, 2013 at 13:27 UTC

    It seems that despite what the docs say, you have to use the descriptive name to set the font.

    Anyhow a better way than destroying and recreating the HList widget, is to clear the contents and than add new contents like in the following example:

    use strict; use warnings; use Tk; use Tk::HList; use Tk::ItemStyle; use Tk::FontDialog; my $mw = MainWindow->new; $mw->title("View"); $mw->minsize(560, 180); my $font = '{Architects Daughter} -12 bold'; # have to use descriptive + name my $top = $mw->Frame()->pack( -side => 'top', -fill => 'both', -expand => +0 ); my $hlistframe = $mw->Frame()->pack( -side => 'top', -fill => 'both', -expand => +1 ); my $bottomframe = $mw->Frame()->pack( -fill => 'x', -expand => 0 ); my $menubutton = $top->Menubutton( -text => "Font", )->pack( -side => 'left' ); $menubutton->command( -label => "Change", -command => sub { $font = $mw->FontDialog->Show; $mw->RefontTree( -font => $font ) if defined $font; print "Font changed: $font\n"; } ); my $hl = $hlistframe->Scrolled( 'HList', -scrollbars => 'ose', -columns => 2, -header => 1, -height => -1, )->pack( -fill => 'both', -expand => 1 ); my $label1 = $hl->Label( -text => "Tool", -anchor => 'w' ); $hl->headerCreate( 0, -itemtype => 'window', -widget => $label1 ); my $label2 = $hl->Label( -text => "Vendor", -anchor => 'w' ); $hl->headerCreate( 1, -itemtype => 'window', -widget => $label2 ); #... $bottomframe->Button( -text => "Add item", -command => \&add_item ) ->pack( -side => 'right', -anchor => 'ne', -fill => 'x' ); $bottomframe->Button( -text => "Clear", -command => \&clear ) ->pack( -side => 'right', -anchor => 'ne', -fill => 'x' ); $mw->RefontTree( -font => $font ) if defined $font; # set default font my $style = $hl->ItemStyle( 'text', -background => 'light green', -font => $font, ); add_item(); sub add_item { my $e = $hl->addchild(''); if (defined $font) { my $font_descriptive = $mw->GetDescriptiveFontName($font); $style->configure( -font => $font ); print $font_descriptive, "\n"; } $hl->itemCreate( $e, 0, -text => 'Col0', -style => $style ); $hl->itemCreate( $e, 1, -text => 'Col1' ); } sub clear { $hl->delete('all'); } MainLoop;

    This way changing the font of the widget is not needed anymore.