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

I'm trying to make a (Windows) text screensaver with Tk::Canvas but I cannot change the text font.

Changing the font with the -font option doesn't do anything:
$canvas->createText($x1, $y1, -font => 'Century', -text => 'Hello world');
It defaults to the (system?) font.

The fonts I'm trying to use are installed in my Windows font folder. The Tk::Canvas documentation refers to Tk_GetFontStruct to get valid fonts but I can't find any information how to do this. Also I can't find any information how to change the font size.

Does anybody know how to change the font (size) in Tk::Canvas on a Windows machine?

Help is much appriciated!

Replies are listed 'Best First'.
Re: Tk::Canvas font problem
by keszler (Priest) on Jun 27, 2004 at 12:54 UTC
    Here's a font spec I've used on Windows, to get 24 point type:
    -font => '-*-MS Sans Serif-Medium-R-Normal-*-*-240-*-*-*-*-*-*'
      Tried that but it remains the (standard) small font.
        This little test prog gives the expected results (two fonts, three sizes) on the Win98 and Win2K boxes here:

        use strict; use warnings; use Tk; my $top = MainWindow->new(); my $canvas = $top->Canvas ()->pack; $canvas->createText(60, 60, -font => '-*-MS Sans Serif-Medium-R-Normal-*-*-240-*-*-*-*-*-*', -text => 'Hello world', ); $canvas->createText(60, 120, -font => '-*-MS Sans Serif-Medium-R-Normal-*-*-120-*-*-*-*-*-*', -text => 'Hello2 world2', ); $canvas->createText(60, 180, -font => '-*-Century-Medium-R-Normal-*-*-180-*-*-*-*-*-*', -text => 'Hello3 world3', ); Tk::MainLoop;
Re: Tk::Canvas font problem
by zentara (Cardinal) on Jun 27, 2004 at 12:57 UTC
    Changing the font with the -font option doesn't do anything: $canvas->createText($x1, $y1, -font 'Century', -text => 'Hello world'); It defaults to the (system?) font.

    I think you need a comma or a => between -font and "Century'

    -font=>'Century'
    (Assuming that is your real code )

    I'm not really a human, but I play one on earth. flash japh
      Sorry, forgot that in my question. In the real code I use a '=>'
Re: Tk::Canvas font problem
by mawe (Hermit) on Jun 27, 2004 at 15:58 UTC
    Hi!

    There is a shorter way to change the fontsize:
    use Tk; my $top = new MainWindow; my $can = $top->Canvas()->pack(); $can->createText(50,10,-text=>"Hello",-font=>"Century 10"); $can->createText(50,50,-text=>"Hello",-font=>"Century 30"); MainLoop();

    Hope this helps. mawe
      Thanks for all the replies everybody. After pulling my hair out I tried my code on an another machine. It worked without major problems. I'm going to do a reinstall on my first machine. Another question: can you recommend any good links or books for learning Perl\Tk? The perldoc aren't really very helpfull to a beginner. (I like the Learning Perl way of explaining things: code examples)