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

i already checked the documentations given on other posts like this, but didn't find what i need.

My problem is: the string 'äöü' gets shown as 'äöü' in Tk Wdgets.

#! /usr/bin/perl -w use Tk; my $mw = MainWindow->new; $mw->Label(-text => 'öäü')->pack; MainLoop;

How do I make those umlauts don't look scrambled?

If this is answered easily, my next question is: how to I make umlauts typed into a Text or Entry look right.

Best regards! Kardan

Replies are listed 'Best First'.
Re: umlauts in Tk widgets
by pc88mxer (Vicar) on Jul 16, 2008 at 16:57 UTC
    You have an encoding problem. Try this:
    use Encode; use Tk; my $bytes = "öäü"; my $text = decode("utf-8", $bytes); my $mw = MainWindow->new; $mw->Label(-text => encode("iso-8859-1", $text))->pack; MainLoop;
    The default font seems to be using a latin-1 mapping.

    How "öäü" is represented will depend on your text editing environment. Mine happens to be UTF-8. Alternatively I could have used use utf8; instead of decode().

    Update: The encoding to iso-8859-1 is not necessary. Tk seems to be one of the very few external modules which is text-aware (yeah!). Both of these invocations will produce the same result:

    $mw->Label(-text => encode("iso-8859-1", $text))->pack; $mw->Label(-text => $text)->pack;
      Great thanks for your replies!

      As i read the given article in german already, i didn't find anything new in the english version.

      I played around with your snippets and updated my script:
      #! /usr/bin/perl -w use strict; use Tk; # use utf8; # <- makes everything even worse use Encode; my $enc = 'utf-8'; my $umlauts = 'öäü'; my $text = decode("utf-8", $umlauts); my $mw = MainWindow->new; # scrambled string widgets $mw->Label(-text => encode($enc, $umlauts))->pack; $mw->Label(-text => decode($enc, $umlauts))->pack; $mw->Label(-text => $umlauts)->pack; $mw->Label(-text => $text)->pack; $mw->Label(-text => encode("iso-8859-1", $umlauts))->pack; # the only one that creates shiny umlauts $mw->Label(-text => encode("iso-8859-1", $text))->pack; MainLoop;
      So now I am able to convert strings loaded from files. Great thanks so far!

      But what about those umlauts typed into Entry or Text and compareable widgets?

      > How "öäü" is represented will depend on your text editing environment.
      How can I get information about this?
Re: umlauts in Tk widgets
by moritz (Cardinal) on Jul 16, 2008 at 16:53 UTC
    It works for me if you pass decoded text strings to Tk.

    So for example if you use UTF-8 for your program source code, also add use utf8; on top.

    See this article, Encode, perluniintro and perlunifaq for more information on that topic.