I'm using Perl/Tk Text widgets to display, edit and update data in a MySQL database. Generally it works OK, with one exception: when I read a string with UTF-8 characters out of the database, it isn't displayed properly in the Text widget. I can paste UTF-8 characters into the text widget, and they do end up in the data base correctly.

So, here's how I try to display stuff from the database:

package photos_unused; require Exporter; use DBI; use photos_globals; use Tk; use Tk::NoteBook; use Tk::Pane; use Tk::JPEG; use Tk::DragDrop; use Tk::DropSite; use Encode qw(encode decode); use strict; . . . # get the photo properties from the data base eval { ($credit, $caption, $extra_text, $out, $original_file_name, $direc +tory) = $dbh->selectrow_array( "SELECT credit, caption, extra_text, outx, original_file_name, + directory " . "FROM photos " . "WHERE photo_num = $photo_num " . "LIMIT 1"); }; #end of eval block; if ($@) { error_message("Database error: $@\n"); } else { $credit = decode('UTF-8', $credit); $caption = decode('UTF-8', $caption); $extra_text = decode('UTF-8', $extra_text); $out = decode('UTF-8', $out); $tl = $mw->Toplevel(); $tl->geometry('500x400'); $tl->title("Photo " . $photo_num . " Properties"); $tl->Label(-text=>"Original file name: " . $original_file_name . " + Directory: " . $directory)->pack(); $fr1 = $tl->Frame(-pady=>5, -padx=>3); $fr1->Label(-text=>"Credit")->pack(-anchor=>'w'); $txtCredit = $fr1->Text(-height=>5)->pack; $txtCredit->insert('end', $credit); $fr1->pack; $fr2 = $tl->Frame(-pady=>5, -padx=>3); $fr2->Label(-text=>"Caption")->pack(-anchor=>'w'); $txtCaption = $fr2->Text(-height=>5)->pack; $txtCaption->insert('end', $caption); $fr2->pack; $fr3 = $tl->Frame(-pady=>5, -padx=>3); $fr3->Label(-text=>"Extra text")->pack(-anchor=>'w'); $txtExtraText = $fr3->Text(-height=>5)->pack; $txtExtraText->insert('end', $extra_text); $fr3->pack; $fr4 = $tl->Frame(-pady=>5, -padx=>3); $btnCancel = $fr4->Button(-text => "Cancel", -command=> sub{$tl- +>destroy})->pack(-side=>'left', -anchor=>'e'); $btnOK = $fr4->Button(-text => "UPDATE", -command=> [\&update_ph +oto_properties, $tl, $photo_num])->pack(-side=>'right', -anchor=>'w') +; $fr4->pack(-fill=>'x'); }

I'm using Perl 5.8.8 and Perl/Tk 804.028 on Ubuntu.

Apologies for the large chunk of code, but I wanted to make sure that there was enough to show how I was going about this.

Thanks!
--- Marais


In reply to Perl/Tk: utf8 in Text widget? by Marais

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.