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

This is probably a trivial problem with a trivial solution, but I can't find any examples on choosing fonts in Perl/Tk. So here's a whittled-down example of my problem.

This script WORKS FINE in Win32 using ActivePerl 5.8.4.

But on Solaris and Linux, a very small number of Chinese characters do not appear properly (in items 124, 125, and 128 in the pulldown menu). Is there a simple explanation for this?

My guess is that, in Windows, the default font happens to cover the Chinese characters I'm using. But since I'm not specifying any particular font in the script, the default font for Solaris/Linux does not cover all the Chinese characters I am using (which would partially explain why it took so long to come back after selecting Chinese characters).

So the simple question is ... how do I specify fonts for Perl/Tk and will this fix the problem?

Another question, was this a decent method of coding or is there a better way? Seems I'll have flexibility issues if I want configurable menu entries (based on user-selections from the menu) because I can't put if-else control inside a menuitem definition.

Thanks,
Perl/Tk novice

(Jan 10: Code replaced with this shorter example -- shockers)

#!/bin/env perl -w use Tk; use Tk::Menu; use Encode::Unicode; use strict; my $exit_val = 0; my $menubar; my $menuitems; my $top; my %code = ( 60 => "Contactor Repair | \x{4FEE}\x{593E}\x{8173}", 120 => "Change Cleaning Disk | \x{66F4}\x{63DB}\x{786C}\x{789F}", 180 => "Change Loadboard | \x{66F4}\x{63DB}\x{6E2C}\x{8A66}\x +{677F}", 240 => "Change LN2 | \x{66F4}\x{63DB}\x{6DB2}\x{614B}\x +{6C2E}", ); #----- Main ---------------------------- $top = new MainWindow; $top -> title ("Select"); $top -> geometry ("200x50+10+10"); $menuitems = [ [Cascade => "SELECT", -menuitems => [ [ Button => "$code{ 60}", -font => "courier", -command => [\&exit_ +r, 60] ], [ Button => "$code{120}", -font => "courier", -command => [\&exit_ +r, 120] ], [ Button => "$code{180}", -font => "courier", -command => [\&exit_ +r, 180] ], [ Button => "$code{240}", -font => "courier", -command => [\&exit_ +r, 240] ], ] ], ]; # end menuitems $menubar = $top->Menu (-menuitems => $menuitems); $top->configure(-menu => $menubar); MainLoop; exit ($exit_val); sub exit_r { $exit_val = shift; $top -> destroy; }

Replies are listed 'Best First'.
Re: Chinese font problem in Perl/Tk
by zentara (Cardinal) on Jan 05, 2005 at 13:28 UTC
    but I can't find any examples on choosing fonts in Perl/Tk.

    Get Tk::FontDialog, it has a real nice FontDialog, and a way to apply the font to your whole window.

    #!/usr/bin/perl use Tk; use Tk::FontDialog; $top=new MainWindow; my $fd; $b = $top->Button(-text => 'Choose Font', -command => sub { $font = $fd->Show; apply_font($font); })->pack; $f = $top->Frame->pack; $f->Label(-text => 'Test RefontTree 1')->pack; $f2 = $f->Frame->pack; $f2->Label(-text => 'Test RefontTree 2')->pack; $c = $f2->Canvas(-width => 100, -height => 30)->pack; $c->createText(0,0,-anchor => 'nw', -text => 'Canvas Text'); $fd = $top->FontDialog(-nicefont => 0, -title => 'Select Font', -applycmd => \&apply_font, -familylabel => 'Schrift~familie', -fixedfontsbutton => 1, -nicefontsbutton => 1, ); $bf = $top->Frame->pack; $bf->Button(-text => 'OK', -command => sub { print "ok $ok\n"; $top->destroy;})->pack(-side => 'left'); $bf->Button(-text => 'Not OK', -command => sub { print "not ok $ok\n"; $top->destroy;})->pack(-side => 'left'); MainLoop; sub apply_font { my $font = shift; if (defined $font) { $b->configure(-font => $font); $f->RefontTree(-font => $font, -canvas => 1); } }

    I'm not really a human, but I play one on earth. flash japh
      Thanks zentara. I tried it out and saw a lot of basic fonts on different Solaris and Linux boxes, but didn't see any Asian characters in there. So without ever specifying a font in my script, I'm wondering what font it chose and how it managed to display (almost all of) the Chinese characters.

      I guess I need to ask even more basic questions:

      I've added some fontCreate stuff from a Slaven Rezic example I saw somewhere and now call out a specific font. But I still don't see how to specify anything differently to get the missing Chinese characters to appear.

      Modified code follows. (It works fine in Windows, so run in Unix to see the problem).

      (Example removed. Refer to code at beginning of thread -- shockers)
        I use Arial Unicode MS from within Perl and Tk applications, see http://mini.net/tcl//11392 for explanations about font itself and http://www.vkonovalov.ru/perlce-tcltk.htm on my few screenshots within Perl+Tcl::Tk using it...

        Must be able using it on Linux as well, although I did not tried namely that font there, because my Tk Unicode apps run on Linux w/o problems even without that font (however I don't have enough Chinese knowledge to notice errors :)

        Best regards,
        Courage, the Cowardly Dog

        but didn't see any Asian characters in there. So without ever specifying a font in my script, I'm wondering what font it chose and how it managed to display (almost all of) the Chinese characters.

        I admit I am not very familiar with unicode and asian fonts. I do remember, however, that when I build and install Tk, one of the tests it runs thru is to fill up a window with alot of asian characters, even though I don't have any asian fonts installed. It probably does it just like you did, specifying the unicode characters with Encode? One of the test scripts in the root of the Tk source tree is called "chfont" and it displays asian characters.

        use strict; use Tk; use Encode qw(decode); my $face = (@ARGV) ? shift : 'fixed'; my @headers = <DATA>; my $mw = MainWindow->new; foreach my $text (@headers) { my $str = decode('MIME-Header' => $text); $mw->Label(-text => $str, -font => [$face,14])->pack; } MainLoop; __DATA__ From: =?Big5?B?qfq8dw==?= <tukb122@ms12.url.com.tw> Subject:=?big5?Q?=A9_=A4b=A5=D8=BF=FD=A7=F3=B7s!!?= Subject: =?Big5?B?o7Ck+S4uLi4uLi4uLi4uLi4uLi4uLg==?= From: =?Big5?B?tW6//bdqtE2k3sC6?= <twpalm@pchome.com.tw> Subject: =?Big5?B?rf6xeqq6uvSvuLVuv/23arRNpN7Aug==?= Subject:=?big5?Q?=A7K=B6O=A5=F4=A5=B4=A4=A4=B4=E4=AA=F8=B3~=B9q=B8=DC? += Subject:=?big5?Q? =A4u=A7@=B2=D6=A4F=A4@=A4=D1=BA=D6=A7Q=B0e=B5=B9=A7A + ^0^ =A6A=A6=B8=B7P=C1=C2=BE=E3=B2z=B3o=A5=F7=B8=EA=AE=C6=AA=BA=A4H +!!! ?= Subject:=?big5?Q? =A4u=A7@=B2=D6=A4F=A4@=A4=D1=BA=D6=A7Q=B0e=B5=B9=A7A + ^0^ =A6A=A6=B8=B7P=C1=C2=BE=E3=B2z=B3o=A5=F7=B8=EA=AE=C6=AA=BA=A4H +!!! ?= Subject:=?big5?Q? =A4u=A7@=B2=D6=A4F=A4@=A4=D1=BA=D6=A7Q=B0e=B5=B9=A7A + ^0^ =A6A=A6=B8=B7P=C1=C2=BE=E3=B2z=B3o=A5=F7=B8=EA=AE=C6=AA=BA=A4H +!!! ?=

        I'm not really a human, but I play one on earth. flash japh