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

I have an application where I want the Tk::TextUndo text widget to be configurable as far as font style and size go. I have the application reading from a config file to get the values when first called but as far as changing the values and capturing those specific values into variables to write them to the config file I am a little confused on.

The documentation is a little lacking and I was wondering if anyone has had any experience with this module?

The modules is called by my $font_set = $mw->FontDialog->Show;. This invokes the font box popup but once changes are made and you hit the "OK" button how can you capture the new settings for annotation?


www.perlskripts.com

Replies are listed 'Best First'.
Re: Understanding Tk::FontDialog?
by keszler (Priest) on Aug 23, 2004 at 23:51 UTC
    The selected font is returned, into $font_set in your example. You then need to reconfigure your widgets to use the font, i.e. $widget->configure( -font => $font_set

    A better method would be to use fontCreate and fontConfigure. This example should point you in the right direction:

    #!/usr/bin/perl # vi: set ts=8 sw=2 et: use warnings; use strict; use Tk; use Data::Dump qw(dump); my $list; my $parent; my $mw = MainWindow->new(); $mw->title("Listbox"); $mw->geometry("300x300+100+100"); $mw->fontCreate( 'lbl', -family => 'arial', -size => '10', -weight + => 'normal' ); $mw->fontCreate( 'lblbtn', -family => 'arial', -size => '14', -weight + => 'bold' ); my $l1 = $mw->Label( -font => 'lbl', -text => "dsal kjhdsa")->pack( -s +ide => 'top', -fill => 'y', -expand => 1 ); my $l2 = $mw->Label( -font => 'lbl', -text => "lkah dsl kj")->pack( -s +ide => 'top', -fill => 'y', -expand => 1 ); my $l3 = $mw->Label( -font => 'lbl', -text => "ih kah klzv")->pack( -s +ide => 'top', -fill => 'y', -expand => 1 ); my $l4 = $mw->Label( -font => 'lbl', -text => "lajd smbl j")->pack( -s +ide => 'top', -fill => 'y', -expand => 1 ); my $l5 = $mw->Label( -font => 'lbl', -text => "lk javh lkj")->pack( -s +ide => 'top', -fill => 'y', -expand => 1 ); $mw->Button( -text => "Change Label-3 Font", -font => 'lblbtn', -command => [ \&chg_font, "widget", $l3 ], )->pack( -side => 'top', -fill => 'none' ); $mw->Button( -text => "Change Label Font", -font => 'lblbtn', -command => [ \&chg_font, "font", "lbl" ], )->pack( -side => 'top', -fill => 'none' ); $mw->Button( -text => "Change Button Font", -font => 'lblbtn', -command => [ \&chg_font, "font", "lblbtn" ], )->pack( -side => 'top', -fill => 'none' ); MainLoop; sub chg_font { my $type = shift; my $which = shift; my $newfont = $mw->FontDialog->Show; if ($type eq "widget") { $which->configure(-font => $newfont); } else { $mw->fontConfigure($which, $mw->fontActual($newfont)); } }

    (Note that once you use the "Change Label-3" button, label 3 will no longer be affected by the "Change Label Font" button.)

      Yeah that works great if I want to change the font values every time I launch the application but I need a way to store the changed values in plain text (non-hex) such as "Courier New" and font size "10" or something.

      www.perlskripts.com
        Take a look at what $mw->fontActual($newfont) returns:

        "-family", "MS Sans Serif", "-size", -11, "-weight", "normal", "-slant", "roman", "-underline", 0, "-overstrike", 0

        You can easily write that to a file, read it back, and use it in the arguments to fontCreate or fontConfigure.

Re: Understanding Tk::FontDialog?
by eserte (Deacon) on Aug 24, 2004 at 09:38 UTC
    There's also the convenience method RefontTree which can apply a font to the whole application. I just added some documentation to Tk::FontDialog. You can look at the current developement version at the SourceForge CVS (please wait a while, anonymous CVS on SourceForge lags behind a couple of hours).