| 1 |
=head1 NAME |
| 2 |
|
| 3 |
Tk::UserGuide - Writing Tk applications in Perl 5 |
| 4 |
|
| 5 |
=for category Introduction |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This document is for beginners. It assumes you know some B<Perl>, and |
| 10 |
have it and Tk running. If you are I<not> currently reading this |
| 11 |
document courtesy of the B<widget> demonstration program, please be |
| 12 |
sure to run B<widget>, as it will show you the various widget types |
| 13 |
supported by Tk and how to use them. B<widget> should be installed in |
| 14 |
your default path, so type I<widget> at a command prompt. |
| 15 |
|
| 16 |
Here are links to other novice tutorials: |
| 17 |
|
| 18 |
L<http://www.lehigh.edu/~sol0/ptk/tpj1.html> |
| 19 |
L<http://www.lehigh.edu/~sol0/ptk/perlmonth01/pm1.html> |
| 20 |
|
| 21 |
I<Mastering Perl/Tk> is the definitive book on Perl/Tk: |
| 22 |
|
| 23 |
L<http://www.oreilly.com/catalog/mastperltk> |
| 24 |
|
| 25 |
=head1 Some Background |
| 26 |
|
| 27 |
Tk GUI programming is event-driven. (This may already be familiar to |
| 28 |
you.) In event-driven programs, the main GUI loop is outside of the |
| 29 |
user program and inside the GUI library. This loop - initiated by |
| 30 |
calling B<MainLoop> - watches all events of interest and activates |
| 31 |
the correct handler procedures to handle these events. Some of these |
| 32 |
handler procedures may be user-supplied; others will be part of the |
| 33 |
library. |
| 34 |
|
| 35 |
For a programmer, this means that you're not watching what is happening; |
| 36 |
instead, you are requested by the toolkit to perform actions whenever |
| 37 |
necessary. |
| 38 |
So, you're not watching for 'raise window / close window / redraw window' |
| 39 |
requests, but you tell the toolkit which routine will handle such cases, |
| 40 |
and the toolkit will call the procedures when required. These procedures |
| 41 |
are known as I<callbacks>, and some of them you write yourself. |
| 42 |
|
| 43 |
=head1 First Requirements |
| 44 |
|
| 45 |
B<Perl> programs that use Tk need to include C<use Tk>. |
| 46 |
A program should also use C<use strict> and the B<-w> switch to ensure |
| 47 |
the program is working without common errors. |
| 48 |
|
| 49 |
Any Perl/Tk application starts by creating the Tk B<L<MainWindow|Tk::MainWindow>>. You |
| 50 |
then create items inside the B<MainWindow>, and/or create new windows |
| 51 |
called B<L<Toplevel|Tk::Toplevel>>s that also contain child items, before starting the |
| 52 |
B<MainLoop>, which is the last logical statment in your program. You |
| 53 |
can also create more items and windows while you're running, using |
| 54 |
callbacks. Items are only shown on the display after they have been |
| 55 |
arranged by a I<geometry manager> like B<L<pack|Tk::pack>>; more information on |
| 56 |
this later. B<MainLoop> starts the GUI and handle all events. That's |
| 57 |
all there is to it! A trivial one-window example is shown below: |
| 58 |
|
| 59 |
#!/usr/bin/perl -w |
| 60 |
use Tk; |
| 61 |
use strict; |
| 62 |
|
| 63 |
my $mw = MainWindow->new; |
| 64 |
$mw->Label(-text => 'Hello, world!')->pack; |
| 65 |
$mw->Button( |
| 66 |
-text => 'Quit', |
| 67 |
-command => sub { exit }, |
| 68 |
)->pack; |
| 69 |
MainLoop; |
| 70 |
|
| 71 |
Please run this example. It shows you two widget types, a B<L<Label|Tk::Label>> |
| 72 |
and a B<L<Button|Tk::Button>>, and how they are packed. When clicked, the B<Button> |
| 73 |
widget invokes the callback specified by the C<-command> option. Finally, |
| 74 |
note the typical Tk style using C<-option> =E<gt> C<value> pairs. |
| 75 |
|
| 76 |
=head1 Widget creation |
| 77 |
|
| 78 |
Tk windows and widgets are hierarchical, S<i.e. one> window includes |
| 79 |
one or more other windows. You create the first Tk window using |
| 80 |
C<MainWindow-E<gt>new>. This returns a window handle, assigned to |
| 81 |
C<$mw> in the example above. Keep track of the main handle, commonly |
| 82 |
called a I<widget reference>. |
| 83 |
|
| 84 |
You can use any Tk handle to create child widgets within the window (or |
| 85 |
widget). This is done by calling the Tk constructor method on the |
| 86 |
variable. In the example above, the C<Label> method called from |
| 87 |
C<$mw> creates a B<Label> widget inside the B<MainWindow>. In the |
| 88 |
constructor call, you can specify various options; you can later add |
| 89 |
or change options for any widget using the B<configure> method, which |
| 90 |
takes the same parameters as the constructor. The one exception to |
| 91 |
the hierarchical structure is the B<Toplevel> constructor, which |
| 92 |
creates a new outermost window. |
| 93 |
|
| 94 |
After you create any widget (other than the B<MainWindow> or |
| 95 |
B<Toplevel>s, you must render it by calling B<pack>. (This is not |
| 96 |
entirely true; more later)). If you do not need to refer to the widget |
| 97 |
after construction and packing, call B<pack> off the constructor |
| 98 |
results, as shown for the B<Label> and B<Button> in the example above. Note |
| 99 |
that the result of the compound call is the result of B<pack>, which |
| 100 |
is a valid Tk handle. |
| 101 |
|
| 102 |
Windows and widgets are deleted by calling B<destroy> on them; |
| 103 |
this will delete and un-draw the widget and all its children. |
| 104 |
|
| 105 |
=head1 Standard Tk widgets |
| 106 |
|
| 107 |
Here is an itemize of the standard Tk widget set. |
| 108 |
|
| 109 |
=over 4 |
| 110 |
|
| 111 |
=item L<Button|Tk::Button> |
| 112 |
|
| 113 |
=item L<Canvas|Tk::Canvas> |
| 114 |
|
| 115 |
=item L<Checkbutton|Tk::Checkbutton> |
| 116 |
|
| 117 |
=item L<Entry|Tk::Entry> |
| 118 |
|
| 119 |
=item L<Frame|Tk::Frame> |
| 120 |
|
| 121 |
=item L<Label|Tk::Label> |
| 122 |
|
| 123 |
=item L<Labelframe|Tk::Labelframe> |
| 124 |
|
| 125 |
=item L<Listbox|Tk::Listbox> |
| 126 |
|
| 127 |
=item L<Menu|Tk::Menu> |
| 128 |
|
| 129 |
=item L<Menubutton|Tk::Menubutton> |
| 130 |
|
| 131 |
=item L<Message|Tk::Message> |
| 132 |
|
| 133 |
=item L<Panedwindow|Tk::Panedwindow> |
| 134 |
|
| 135 |
=item L<Radiobutton|Tk::Radiobutton> |
| 136 |
|
| 137 |
=item L<Scale|Tk::Scale> |
| 138 |
|
| 139 |
=item L<Scrollbar|Tk::Scrollbar> |
| 140 |
|
| 141 |
=item L<Spinbox|Tk::Spinbox> |
| 142 |
|
| 143 |
=item L<Text|Tk::Text> |
| 144 |
|
| 145 |
=item L<Toplevel|Tk::Toplevel> |
| 146 |
|
| 147 |
=back |
| 148 |
|
| 149 |
Perl/Tk provides an equal number of new widgets, above and beyond this |
| 150 |
core set. |
| 151 |
|
| 152 |
=over 4 |
| 153 |
|
| 154 |
=item L<Adjuster|Tk::Adjuster> |
| 155 |
|
| 156 |
=item L<Balloon|Tk::Balloon> |
| 157 |
|
| 158 |
=item L<BrowseEntry|Tk::BrowseEntry> |
| 159 |
|
| 160 |
=item L<ColorEditor|Tk::ColorEditor> |
| 161 |
|
| 162 |
=item L<Dialog|Tk::Dialog> |
| 163 |
|
| 164 |
=item L<DialogBox|Tk::DialogBox> |
| 165 |
|
| 166 |
=item L<DirTree|Tk::DirTree> |
| 167 |
|
| 168 |
=item ErrorDialog |
| 169 |
|
| 170 |
=item L<FBox|Tk::FBox> |
| 171 |
|
| 172 |
=item L<FileSelect|Tk::FileSelect> |
| 173 |
|
| 174 |
=item L<HList|Tk::HList> |
| 175 |
|
| 176 |
=item LabEntry |
| 177 |
|
| 178 |
=item L<LabFrame|Tk::LabFrame> |
| 179 |
|
| 180 |
=item L<NoteBook|Tk::NoteBook> |
| 181 |
|
| 182 |
=item L<Optionmenu|Tk::Optionmenu> |
| 183 |
|
| 184 |
=item L<Pane|Tk::Pane> |
| 185 |
|
| 186 |
=item L<ProgressBar|Tk::ProgressBar> |
| 187 |
|
| 188 |
=item L<ROText|Tk::ROText> |
| 189 |
|
| 190 |
=item L<Table|Tk::Table> |
| 191 |
|
| 192 |
=item L<TextUndo|Tk::TextUndo> |
| 193 |
|
| 194 |
=item L<Tiler|Tk::Tiler> |
| 195 |
|
| 196 |
=item L<TList|Tk::TList> |
| 197 |
|
| 198 |
=item L<Tree|Tk::Tree> |
| 199 |
|
| 200 |
=back |
| 201 |
|
| 202 |
=head1 Variables and callback routines |
| 203 |
|
| 204 |
Most graphical interfaces are used to set up a set of values and conditions, |
| 205 |
and then perform the appropriate action. The Tk toolkit is different |
| 206 |
from your average text-based prompting or menu driven system in that you do |
| 207 |
not collect settings yourself, and decide on an action based on an |
| 208 |
input code; instead, you leave these |
| 209 |
values to your toolkit and only get them when the action is performed. |
| 210 |
|
| 211 |
So, where a traditional text-based system would look like this: |
| 212 |
|
| 213 |
#!/usr/bin/perl -w |
| 214 |
use strict; |
| 215 |
|
| 216 |
print "Please type a font name\n"; |
| 217 |
my $font = <>; chomp $font; |
| 218 |
# Validate font |
| 219 |
|
| 220 |
print "Please type a file name\n"; |
| 221 |
my $filename = <>; chomp $filename; |
| 222 |
# Validate filename |
| 223 |
|
| 224 |
print "Type <1> to fax, <2> to print\n"; |
| 225 |
my $option = <>; chomp $option; |
| 226 |
if ($option eq 1) { |
| 227 |
print "Faxing $filename in font $font\n"; |
| 228 |
} elsif ($option eq 2) { |
| 229 |
print "Now sending $filename to printer in font $font\n"; |
| 230 |
} |
| 231 |
|
| 232 |
The slightly larger example below shows how to do this in Tk. |
| 233 |
Note the use of callbacks. Note, also, that Tk handles the values, and |
| 234 |
the subroutine uses the method B<get> to get at the values. |
| 235 |
If a user changes his mind and wants to change the font again, |
| 236 |
the application never notices; it's all handled by Tk. |
| 237 |
|
| 238 |
#!/usr/bin/perl -w |
| 239 |
use Tk; |
| 240 |
use strict; |
| 241 |
|
| 242 |
my $mw = MainWindow->new; |
| 243 |
|
| 244 |
$mw->Label(-text => 'File Name')->pack; |
| 245 |
my $filename = $mw->Entry(-width => 20); |
| 246 |
$filename->pack; |
| 247 |
|
| 248 |
$mw->Label(-text => 'Font Name')->pack; |
| 249 |
my $font = $mw->Entry(-width => 10); |
| 250 |
$font->pack; |
| 251 |
|
| 252 |
$mw->Button( |
| 253 |
-text => 'Fax', |
| 254 |
-command => sub{do_fax($filename, $font)} |
| 255 |
)->pack; |
| 256 |
|
| 257 |
$mw->Button( |
| 258 |
-text => 'Print', |
| 259 |
-command => sub{do_print($filename, $font)} |
| 260 |
)->pack; |
| 261 |
|
| 262 |
MainLoop; |
| 263 |
|
| 264 |
sub do_fax { |
| 265 |
my ($file, $font) = @_; |
| 266 |
my $file_val = $file->get; |
| 267 |
my $font_val = $font->get; |
| 268 |
print "Now faxing $file_val in font $font_val\n"; |
| 269 |
} |
| 270 |
|
| 271 |
sub do_print { |
| 272 |
my ($file, $font) = @_; |
| 273 |
my $file_val = $file->get; |
| 274 |
my $font_val = $font->get; |
| 275 |
print "Sending file $file_val to printer in font $font_val\n"; |
| 276 |
} |
| 277 |
|
| 278 |
=head1 The packer - grouping with Frame widgets |
| 279 |
|
| 280 |
In the examples above, you must have noticed the B<pack> calls. This |
| 281 |
is one of the more complicated parts of Tk. The basic idea is that |
| 282 |
any window or widget should be subject to a Tk geometry manager; the |
| 283 |
I<packer> is one of the placement managers, and B<L<grid|Tk::grid>> is another. |
| 284 |
|
| 285 |
The actions of the packer are rather simple: when applied |
| 286 |
to a widget, the packer positions that widget on the indicated position |
| 287 |
within the remaining space in its parent. By default, the position is |
| 288 |
on top; this means the next items will be put below. You can also |
| 289 |
specify the left, right, or bottom positions. Specify position |
| 290 |
using B<-side =E<gt> 'right'>. |
| 291 |
|
| 292 |
Additional packing parameters specify the behavior of the widget when |
| 293 |
there is some space left in the B<L<Frame|Tk::Frame>> or when the window size is |
| 294 |
increased. If widgets should maintain a fixed size, specify nothing; |
| 295 |
this is the default. For widgets that you want to fill up the current |
| 296 |
horizontal and/or vertical space, specify B<-fill =E<gt> 'x'>, B<'y'>, |
| 297 |
or B<'both'>; for widgets that should grow, specify B<-expand =E<gt> 1>. |
| 298 |
These parameters are not shown in the example below; see the B<widget> |
| 299 |
demonstration. |
| 300 |
|
| 301 |
If you want to group some items within a window that have a different |
| 302 |
packing order than others, you can include them in a Frame. This is a |
| 303 |
do-nothing window type that is meant for packing or filling (and to |
| 304 |
play games with borders and colors). |
| 305 |
|
| 306 |
The example below shows the use of pack and Frames: |
| 307 |
|
| 308 |
#!/usr/bin/perl -w |
| 309 |
use Tk; |
| 310 |
use strict; |
| 311 |
|
| 312 |
# Take top and the bottom - now implicit top is in the middle |
| 313 |
my $mw = MainWindow->new; |
| 314 |
$mw->title( 'The MainWindow' ); |
| 315 |
$mw->Label(-text => 'At the top (default)')->pack; |
| 316 |
$mw->Label(-text => 'At the bottom')->pack(-side => 'bottom'); |
| 317 |
$mw->Label(-text => 'The middle remains')->pack; |
| 318 |
|
| 319 |
# Since left and right are taken, bottom will not work... |
| 320 |
my $top1 = $mw->Toplevel; |
| 321 |
$top1->title( 'Toplevel 1' ); |
| 322 |
$top1->Label(-text => 'Left')->pack(-side => 'left'); |
| 323 |
$top1->Label(-text => 'Right')->pack(-side => 'right'); |
| 324 |
$top1->Label(-text => '?Bottom?')->pack(-side => 'bottom'); |
| 325 |
|
| 326 |
# But when you use Frames, things work quite alright |
| 327 |
my $top2 = $mw->Toplevel; |
| 328 |
$top2->title( 'Toplevel 2' ); |
| 329 |
my $frame = $top2->Frame; |
| 330 |
$frame->pack; |
| 331 |
$frame->Label(-text => 'Left2')->pack(-side => 'left'); |
| 332 |
$frame->Label(-text => 'Right2')->pack(-side => 'right'); |
| 333 |
$top2->Label(-text => 'Bottom2')->pack(-side => 'bottom'); |
| 334 |
|
| 335 |
MainLoop; |
| 336 |
|
| 337 |
=head1 More than one window |
| 338 |
|
| 339 |
Most real applications require more than one window. As you just saw, |
| 340 |
you can create more outermost windows by using a B<Toplevel> widget. |
| 341 |
Each window is independent; destroying a B<Toplevel> window does not |
| 342 |
affect the others as long as they are not a child of the closed |
| 343 |
B<Toplevel>. However, exiting the B<MainWindow> will destroy all remaining |
| 344 |
B<Toplevel> widgets and end the application. The |
| 345 |
example below shows a trivial three-window application: |
| 346 |
|
| 347 |
#!/usr/bin/perl -w |
| 348 |
use Tk; |
| 349 |
use strict; |
| 350 |
|
| 351 |
my $mw = MainWindow->new; |
| 352 |
fill_window($mw, 'Main'); |
| 353 |
my $top1 = $mw->Toplevel; |
| 354 |
fill_window($top1, 'First top-level'); |
| 355 |
my $top2 = $mw->Toplevel; |
| 356 |
fill_window($top2, 'Second top-level'); |
| 357 |
MainLoop; |
| 358 |
|
| 359 |
sub fill_window { |
| 360 |
my ($window, $header) = @_; |
| 361 |
$window->Label(-text => $header)->pack; |
| 362 |
$window->Button( |
| 363 |
-text => 'close', |
| 364 |
-command => [$window => 'destroy'] |
| 365 |
)->pack(-side => 'left'); |
| 366 |
$window->Button( |
| 367 |
-text => 'exit', |
| 368 |
-command => [$mw => 'destroy'] |
| 369 |
)->pack(-side => 'right'); |
| 370 |
} |
| 371 |
|
| 372 |
=head1 More callbacks |
| 373 |
|
| 374 |
So far, all callback routines shown called a user procedure. |
| 375 |
You can also have a callback routine call another Tk routine. |
| 376 |
This is the way that scroll bars are implemented: scroll-bars |
| 377 |
can call a Tk item or a user procedure, whenever their position |
| 378 |
has changed. The Tk item that has a scrollbar attached calls the |
| 379 |
scrollbar when its size or offset has changed. In this way, |
| 380 |
the items are linked. You can still ask a scrollbar's position, |
| 381 |
or set it by hand - but the defaults will be taken care of. |
| 382 |
|
| 383 |
The example below shows a B<L<Listbox|Tk::Listbox>> with a scroll bar. Moving |
| 384 |
the scrollbar moves the B<Listbox>. Scanning a B<Listbox> (dragging |
| 385 |
an item with the left mouse button) moves the scrollbar. |
| 386 |
|
| 387 |
#!/usr/bin/perl -w |
| 388 |
use Tk; |
| 389 |
use strict; |
| 390 |
|
| 391 |
my $mw = MainWindow->new; |
| 392 |
my $box = $mw->Listbox( |
| 393 |
-relief => 'sunken', |
| 394 |
-height => 5, |
| 395 |
-setgrid => 1, |
| 396 |
); |
| 397 |
my @items = qw(One Two Three Four Five Six Seven |
| 398 |
Eight Nine Ten Eleven Twelve); |
| 399 |
foreach (@items) { |
| 400 |
$box->insert('end', $_); |
| 401 |
} |
| 402 |
my $scroll = $mw->Scrollbar(-command => ['yview', $box]); |
| 403 |
$box->configure(-yscrollcommand => ['set', $scroll]); |
| 404 |
$box->pack(-side => 'left', -fill => 'both', -expand => 1); |
| 405 |
$scroll->pack(-side => 'right', -fill => 'y'); |
| 406 |
|
| 407 |
MainLoop; |
| 408 |
|
| 409 |
Note that there's a convenience method B<L<Scrolled|Tk::Scrolled>> |
| 410 |
which helps constructing widgets with automatically managed |
| 411 |
scrollbars. |
| 412 |
|
| 413 |
=head1 Canvases and tags |
| 414 |
|
| 415 |
One of the most powerful widgets in Tk is the B<L<Canvas|Tk::Canvas>> window. |
| 416 |
In a B<Canvas> window, you can draw simple graphics and include |
| 417 |
other widgets. The B<Canvas> area may be larger than the visible window, |
| 418 |
and may then be scrolled. Any item you draw on the canvas has its own id, |
| 419 |
and may optionally have one or more I<tags>. You may refer to any |
| 420 |
item by its id, and may refer to any group of items by a common tag; |
| 421 |
you can move, delete, or change groups of items using these tags, |
| 422 |
and you can I<bind> actions to tags. For a properly designed (often |
| 423 |
structured) B<Canvas>, you can specify powerful actions quite simply. |
| 424 |
|
| 425 |
In the example below, actions are bound to circles (single click) |
| 426 |
and blue items (double-click); obviously, this can be extended to any |
| 427 |
tag or group of tags. |
| 428 |
|
| 429 |
#!/usr/bin/perl -w |
| 430 |
use Tk; |
| 431 |
use strict; |
| 432 |
|
| 433 |
# Create B<MainWindow> and canvas |
| 434 |
my $mw = MainWindow->new; |
| 435 |
my $canvas = $mw->Canvas; |
| 436 |
$canvas->pack(-expand => 1, -fill => 'both'); |
| 437 |
|
| 438 |
# Create various items |
| 439 |
create_item($canvas, 1, 1, 'circle', 'blue', 'Jane'); |
| 440 |
create_item($canvas, 4, 4, 'circle', 'red', 'Peter'); |
| 441 |
create_item($canvas, 4, 1, 'square', 'blue', 'James'); |
| 442 |
create_item($canvas, 1, 4, 'square', 'red', 'Patricia'); |
| 443 |
|
| 444 |
# Single-clicking with left on a 'circle' item invokes a procedure |
| 445 |
$canvas->bind('circle', '<1>' => sub {handle_circle($canvas)}); |
| 446 |
# Double-clicking with left on a 'blue' item invokes a procedure |
| 447 |
$canvas->bind('blue', '<Double-1>' => sub {handle_blue($canvas)}); |
| 448 |
MainLoop; |
| 449 |
|
| 450 |
# Create an item; use parameters as tags (this is not a default!) |
| 451 |
sub create_item { |
| 452 |
my ($can, $x, $y, $form, $color, $name) = @_; |
| 453 |
|
| 454 |
my $x2 = $x + 1; |
| 455 |
my $y2 = $y + 1; |
| 456 |
my $kind; |
| 457 |
$kind = 'oval' if ($form eq 'circle'); |
| 458 |
$kind = 'rectangle' if ($form eq 'square'); |
| 459 |
$can->create( |
| 460 |
($kind, "$x" . 'c', "$y" . 'c', |
| 461 |
"$x2" . 'c', "$y2" . 'c'), |
| 462 |
-tags => [$form, $color, $name], |
| 463 |
-fill => $color); |
| 464 |
} |
| 465 |
|
| 466 |
# This gets the real name (not current, blue/red, square/circle) |
| 467 |
# Note: you'll want to return a list in realistic situations... |
| 468 |
sub get_name { |
| 469 |
my ($can) = @_; |
| 470 |
my $item = $can->find('withtag', 'current'); |
| 471 |
my @taglist = $can->gettags($item); |
| 472 |
my $name; |
| 473 |
foreach (@taglist) { |
| 474 |
next if ($_ eq 'current'); |
| 475 |
next if ($_ eq 'red' or $_ eq 'blue'); |
| 476 |
next if ($_ eq 'square' or $_ eq 'circle'); |
| 477 |
$name = $_; |
| 478 |
last; |
| 479 |
} |
| 480 |
return $name; |
| 481 |
} |
| 482 |
|
| 483 |
sub handle_circle { |
| 484 |
my ($can) = @_; |
| 485 |
my $name = get_name($can); |
| 486 |
print "Action on circle $name...\n"; |
| 487 |
} |
| 488 |
|
| 489 |
sub handle_blue { |
| 490 |
my ($can) = @_; |
| 491 |
my $name = get_name($can); |
| 492 |
print "Action on blue item $name...\n"; |
| 493 |
} |
| 494 |
|
| 495 |
=head1 Perl/Tk and Unicode |
| 496 |
|
| 497 |
Perl/Tk follows Perl's model of handling Unicode. That is, if a string |
| 498 |
is correctly flagged as a E<quot>characterE<quot> string in the sense |
| 499 |
like described in L<Encode/TERMINOLOGY>, then Perl/Tk will very |
| 500 |
probably display and handle this string correctly. |
| 501 |
|
| 502 |
Note that every variable which is passed somehow into a Perl/Tk method |
| 503 |
will be implicitely changed into an internally utf8-flagged variable. |
| 504 |
Semantically nothing changes, as the series of codepoints stays the |
| 505 |
same, but things will change when variables with high-bit iso-8859-1 |
| 506 |
characters will be passed to the "outer" world. In this case you have |
| 507 |
to explicitely mark the encoding of your output stream if using IO, or |
| 508 |
encode the variables using L<Encode> for other style of communication. |
| 509 |
|
| 510 |
This is the theory, now some examples. |
| 511 |
|
| 512 |
If you use non-iso-8859-1 characters in the source code, then use |
| 513 |
either the C<use utf8;> or C<use encoding 'I<encodingname>'> pragma: |
| 514 |
|
| 515 |
use utf8; |
| 516 |
use Tk; |
| 517 |
my $x = "some characters using utf8 encoding"; |
| 518 |
tkinit->Label(-text => $x)->pack; |
| 519 |
MainLoop; |
| 520 |
|
| 521 |
For data that comes from a file you have to specify the encoding |
| 522 |
unless it's encoded as ascii or iso-8559-1: |
| 523 |
|
| 524 |
use Tk; |
| 525 |
open my $FH, "<:encoding(utf-8)", "filename" or die $!; |
| 526 |
# or for utf-16 data: open my $FH, "<:encoding(utf-16)", "filename" or die $!; |
| 527 |
my $data = <$FH>; |
| 528 |
tkinit->Label(-text => $data)->pack; |
| 529 |
MainLoop; |
| 530 |
|
| 531 |
Likewise, the encoding must be specified for all data which is read |
| 532 |
from Tk widgets and that shall be output into a file. For the output, |
| 533 |
the encoding should be always specified, even if it is iso-8859-1: |
| 534 |
|
| 535 |
use Tk; |
| 536 |
$mw = tkinit; |
| 537 |
$mw->Entry(-textvariable => \$input)->pack; |
| 538 |
$mw->Button( |
| 539 |
-text => "Write to file", |
| 540 |
-command => sub { |
| 541 |
open my $FH, ">:encoding(iso-8859-1)", "filename" or die $!; |
| 542 |
print $FH $input; |
| 543 |
}, |
| 544 |
)->pack; |
| 545 |
MainLoop; |
| 546 |
|
| 547 |
Note that Tk is Unicode-capable. So you need to be prepared that the |
| 548 |
user has the appropriate input methods activated to enter non-ascii |
| 549 |
characters. If an output encoding is used which does not cover the |
| 550 |
whole of Unicode codepoints then a warning will be issued when writing |
| 551 |
the file, like this: |
| 552 |
|
| 553 |
"\x{20ac}" does not map to iso-8859-1 at /usr/local/lib/perl5/site_perl/5.8.8/mach/Tk.pm line 250. |
| 554 |
|
| 555 |
Also, the same hexadecimal notation will be used as replacements for |
| 556 |
the unhandled characters. |
| 557 |
|
| 558 |
Handling encoding in I/O is pretty simple using the C<encoding> PerlIO |
| 559 |
layer, as described above. In other cases, such as when dealing with |
| 560 |
databases, encoding the data usually has to be done manually, unless |
| 561 |
the database driver has some means for automatically do this for you. |
| 562 |
So when working with a MySQL database, one could use: |
| 563 |
|
| 564 |
use Tk; |
| 565 |
use DBI; |
| 566 |
use Encode qw(encode); |
| 567 |
$mw = tkinit; |
| 568 |
$mw->Entry(-textvariable => \$input)->pack; |
| 569 |
$mw->Button( |
| 570 |
-text => "Write to database", |
| 571 |
-command => sub { |
| 572 |
my $dbh = DBI->connect("dbi:mysql:test", "root", "") or die; |
| 573 |
my $encoded_input = encode("iso-8859-1", $input); |
| 574 |
$dbh->do("INSERT INTO testtable VALUES (?)", undef, $encoded_input) or die; |
| 575 |
}, |
| 576 |
)->pack; |
| 577 |
MainLoop; |
| 578 |
|
| 579 |
Unfortunately, there are still places in Perl ignorant of Unicode. One |
| 580 |
of these places are filenames. Consequently, the file selectors in |
| 581 |
Perl/Tk do not handle encoding of filenames properly. Currently they |
| 582 |
suppose that filenames are in iso-8859-1 encoding, at least on Unix |
| 583 |
systems. As soon as Perl has a concept of filename encodings, then |
| 584 |
Perl/Tk will also implement such schemes. |
| 585 |
|
| 586 |
=cut |