in reply to Tk Entry & Right Single Quote

Folks-

As usual, the final solution was a bit more complicated than I thought, but I wanted to provide a final update in case others have the same issue.

The attached code shows what I did in order to translate unicode characters into ASCII with Text::Unidecode. This will translate the entire string on each keystroke, and works with cut-n-paste as well.

NOTE: when using a text variable for the text in the entry, you cannot simply re-assign text to it in the validatecommand() sub. One trick is to use afterIdle() to do that later.

Thanks again to all for the help!

Craig

#!/opt/homebrew/bin/perl use strict; use warnings; use Data::Dumper; use Tk; use utf8; use Text::Unidecode; my $mw = MainWindow->new(); my $textvar; my $e = $mw->Entry(-textvariable => \$textvar, -validate => 'key', -validatecommand => sub { my ($new,$changed,$old,$ix,$type) = @_; return 1 if (!defined($changed)); return 1 if ($new eq "") or ($type<0); $mw->afterIdle(sub{ $textvar = unidecode($new); print STDERR "ASCII: $textvar\n"; }); return 1; }, )->pack(); MainLoop;

Replies are listed 'Best First'.
Re: My Solution: Tk Entry & Right Single Quote
by cmv (Chaplain) on Apr 24, 2024 at 20:48 UTC
    Folks-

    One other thing...

    If you are trying to create a perl executable of the above script using par (with either pp or pp_autolink, the following will not work:

    pp_autolink -v -o squote.exe squote.pl

    This is because the translation tables are not being found and included. To fix this issue, use the following command:

    pp_autolink -v -M Text::Unidecode:: -o squote.exe squote.pl

    The trailing :: tells par to include both the file Text::Unidecode.pl and the directory Text::Unidecode which contains all of the necessary translation tables.

    I wanted to document this here as well, so when I forget later on - I'll have a chance of catching it again.

    -Craig