Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "USERGUIDE 1" | |
132 | .TH USERGUIDE 1 "2000-12-30" "perl v5.8.0" "User Contributed Perl Documentation" | |
133 | .SH "NAME" | |
134 | perl/Tk \- Writing Tk applications in perl5. | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | This manual page is for beginners. It assumes you know some perl, | |
138 | and have got perl+Tk running. | |
139 | Please run the 'widget' demo before reading this text; it will teach you | |
140 | the various widget types supported by Tk. | |
141 | .SH "Some background" | |
142 | .IX Header "Some background" | |
143 | Tk \s-1GUI\s0 programming is event\-driven. (This may already be familiar to you.) | |
144 | In event-driven programs, the main \s-1GUI\s0 loop is outside of the user program | |
145 | and inside the \s-1GUI\s0 library. This loop will watch all events of interest, | |
146 | and activate the correct handler procedures to handle these events. | |
147 | Some of these handler procedures may be user\-supplied; others will be part | |
148 | of the library. | |
149 | .PP | |
150 | For a programmer, this means that you're not watching what is happening; | |
151 | instead, you are requested by the toolkit to perform actions whenever | |
152 | necessary. | |
153 | So, you're not watching for 'raise window / close window / redraw window' | |
154 | requests, but you tell the toolkit which routine will handle such cases, | |
155 | and the toolkit will call the procedures when required. | |
156 | .SH "First requirements" | |
157 | .IX Header "First requirements" | |
158 | Any perl program that uses Tk needs to include \f(CW\*(C`use Tk\*(C'\fR. | |
159 | A program should also use \f(CW\*(C`use strict\*(C'\fR and the \fB\-w\fR switch to ensure | |
160 | the program is working without common errors. | |
161 | .PP | |
162 | Any Tk application starts by creating the Tk main window. You then create | |
163 | items inside the main window, or create new windows, before starting the | |
164 | mainloop. | |
165 | (You can also create more items and windows while you're running.) | |
166 | The items will be shown on the display after you \f(CW\*(C`pack\*(C'\fR them; | |
167 | more info on this later. | |
168 | Then you do a Tk mainloop; this will start the \s-1GUI\s0 and handle all events. | |
169 | That's your application. | |
170 | A trivial one-window example is show below: | |
171 | .PP | |
172 | .Vb 1 | |
173 | \& #! /usr/bin/perl5 -w | |
174 | .Ve | |
175 | .PP | |
176 | .Vb 2 | |
177 | \& use strict; | |
178 | \& use Tk; | |
179 | .Ve | |
180 | .PP | |
181 | .Vb 6 | |
182 | \& my $main = MainWindow->new; | |
183 | \& $main->Label(-text => 'Hello, world!')->pack; | |
184 | \& $main->Button(-text => 'Quit', | |
185 | \& -command => [$main => 'destroy'] | |
186 | \& )->pack; | |
187 | \& MainLoop; | |
188 | .Ve | |
189 | .PP | |
190 | Please run this example. It shows you two items types also shown in the | |
191 | widget demo; it also shows you how items are created and packed. | |
192 | Finally, note the typical Tk style using \f(CW\*(C`\-option\*(C'\fR => \f(CW\*(C`value\*(C'\fR pairs. | |
193 | .SH "Item creation" | |
194 | .IX Header "Item creation" | |
195 | Tk windows and widgets are hierarchical, i.e.\ one includes one or more | |
196 | others. You create the first Tk window using \f(CW\*(C`MainWindow\->new\*(C'\fR. | |
197 | This returns a window handle, assigned to \f(CW$main\fR in the example above. | |
198 | Keep track of the main handle. | |
199 | .PP | |
200 | You can use any Tk handle to create sub-items within the window or widget. | |
201 | This is done by calling the Tk constructor method on the variable. | |
202 | In the example above, the \f(CW\*(C`Label\*(C'\fR method called from \f(CW$main\fR creates a | |
203 | label widget inside the main window. In the constructor call, you can specify | |
204 | various options; you can later add or change options for any widget | |
205 | using the \f(CW\*(C`configure\*(C'\fR method, which takes the same parameters as the | |
206 | constructor. | |
207 | The one exception to the hierarchical structure is the \f(CW\*(C`Toplevel\*(C'\fR constructor, | |
208 | which creates a new outermost window. | |
209 | .PP | |
210 | After you create any widget, you must render it by calling \f(CW\*(C`pack\*(C'\fR. (This | |
211 | is not entirely true; more info later). If you do not need to refer to | |
212 | the widget after construction and packing, call \f(CW\*(C`pack\*(C'\fR off the constructor | |
213 | results, as shown for the label and button in the example above. | |
214 | Note that the result of the compound call is the result of \f(CW\*(C`pack\*(C'\fR, | |
215 | which is a valid Tk handle. | |
216 | .PP | |
217 | Windows and widgets are deleted by calling \f(CW\*(C`destroy\*(C'\fR on them; | |
218 | this will delete and un-draw the widget and all its children. | |
219 | .SH "Standard Tk types" | |
220 | .IX Header "Standard Tk types" | |
221 | .IP "Button" 4 | |
222 | .IX Item "Button" | |
223 | .PD 0 | |
224 | .IP "Radiobutton" 4 | |
225 | .IX Item "Radiobutton" | |
226 | .IP "Checkbutton" 4 | |
227 | .IX Item "Checkbutton" | |
228 | .IP "Listbox" 4 | |
229 | .IX Item "Listbox" | |
230 | .IP "Scrollbar" 4 | |
231 | .IX Item "Scrollbar" | |
232 | .IP "Entry" 4 | |
233 | .IX Item "Entry" | |
234 | .IP "Text" 4 | |
235 | .IX Item "Text" | |
236 | .IP "Canvas" 4 | |
237 | .IX Item "Canvas" | |
238 | .IP "Frame" 4 | |
239 | .IX Item "Frame" | |
240 | .IP "Toplevel" 4 | |
241 | .IX Item "Toplevel" | |
242 | .IP "Scale" 4 | |
243 | .IX Item "Scale" | |
244 | .IP "Menu" 4 | |
245 | .IX Item "Menu" | |
246 | .IP "Menubutton" 4 | |
247 | .IX Item "Menubutton" | |
248 | .PD | |
249 | .SH "Variables and callback routines" | |
250 | .IX Header "Variables and callback routines" | |
251 | Most graphical interfaces are used to set up a set of values and conditions, | |
252 | and then perform the appropriate action. The Tk toolkit is different | |
253 | from your average text-based prompting or menu driven system in that you do | |
254 | not collect settings yourself, and decide on an action based on an | |
255 | input code; instead, you leave these | |
256 | values to your toolkit and only get them when the action is performed. | |
257 | .PP | |
258 | So, where a traditional text-based system would look like this: | |
259 | (yes, this is obviously dumb code) | |
260 | .PP | |
261 | .Vb 1 | |
262 | \& #! /usr/bin/perl5 -w | |
263 | .Ve | |
264 | .PP | |
265 | .Vb 1 | |
266 | \& use strict; | |
267 | .Ve | |
268 | .PP | |
269 | .Vb 3 | |
270 | \& print "Please type a font name\en"; | |
271 | \& my $font = <>; chomp $font; | |
272 | \& # Validate font | |
273 | .Ve | |
274 | .PP | |
275 | .Vb 3 | |
276 | \& print "Please type a file name\en"; | |
277 | \& my $filename = <>; chomp $filename; | |
278 | \& # Validate filename | |
279 | .Ve | |
280 | .PP | |
281 | .Vb 7 | |
282 | \& print "Type <1> to fax, <2> to print\en"; | |
283 | \& my $option = <>; chomp $option; | |
284 | \& if ($option eq 1) { | |
285 | \& print "Faxing $filename in font $font\en"; | |
286 | \& } elsif ($option eq 2) { | |
287 | \& print "Now sending $filename to printer in font $font\en"; | |
288 | \& } | |
289 | .Ve | |
290 | .PP | |
291 | The (slightly larger) example below shows how to do this is Tk. | |
292 | Note the use of callbacks. Note, also, that Tk handles the values, and | |
293 | the subroutine uses \f(CW\*(C`get\*(C'\fR to get at the values. | |
294 | If a user changes his mind and wants to change the font again, | |
295 | the application never notices; it's all handled by Tk. | |
296 | .PP | |
297 | .Vb 1 | |
298 | \& #! /usr/bin/perl5 -w | |
299 | .Ve | |
300 | .PP | |
301 | .Vb 2 | |
302 | \& use strict; | |
303 | \& use Tk; | |
304 | .Ve | |
305 | .PP | |
306 | .Vb 13 | |
307 | \& my $main = MainWindow->new; | |
308 | \& $main->Label(-text => 'Print file')->pack; | |
309 | \& my $font = $main->Entry(-width => 10); | |
310 | \& $font->pack; | |
311 | \& my $filename = $main->Entry(-width => 10); | |
312 | \& $filename->pack; | |
313 | \& $main->Button(-text => 'Fax', | |
314 | \& -command => sub{do_fax($filename, $font)} | |
315 | \& )->pack; | |
316 | \& $main->Button(-text => 'Print', | |
317 | \& -command => sub{do_print($filename, $font)} | |
318 | \& )->pack; | |
319 | \& MainLoop; | |
320 | .Ve | |
321 | .PP | |
322 | .Vb 6 | |
323 | \& sub do_fax { | |
324 | \& my ($file, $font) = @_; | |
325 | \& my $file_val = $file->get; | |
326 | \& my $font_val = $font->get; | |
327 | \& print "Now faxing $file_val in $font_val\en"; | |
328 | \& } | |
329 | .Ve | |
330 | .PP | |
331 | .Vb 6 | |
332 | \& sub do_print { | |
333 | \& my ($file, $font) = @_; | |
334 | \& my $file_val = $file->get; | |
335 | \& my $font_val = $font->get; | |
336 | \& print "Sending file $file_val to printer in $font_val\en"; | |
337 | \& } | |
338 | .Ve | |
339 | .SH "The packer. Grouping and frames." | |
340 | .IX Header "The packer. Grouping and frames." | |
341 | In the examples above, you must have noticed the pack calls. | |
342 | This is one of the more complicated parts of Tk. The basic idea | |
343 | is that any window or widget should be subject to a Tk widget placement manager; | |
344 | the \fIpacker\fR is one of the placement managers. | |
345 | .PP | |
346 | The actions of the packer are rather simple: when applied | |
347 | to a widget, the packer positions that widget on the indicated position | |
348 | within the remaining space in its parent. By default, the position is | |
349 | on top; this means the next items will be put below. You can also | |
350 | specify the left, right, or bottom positions. Specify position | |
351 | using \fB\-side => 'right'\fR. | |
352 | .PP | |
353 | Additional packing parameters specify the behavior of the widget when | |
354 | there is some space left in the frame or when the window size is | |
355 | increased. If widgets should maintain a fixed size, specify nothing; | |
356 | this is the default. For widgets that you want to fill up the current | |
357 | horizontal space, specify \fB\-fill => 'x'\fR, \fBy\fR, or \fBboth\fR; for | |
358 | widgets that should grow, specify \fB\-expand => 1\fR. These | |
359 | parameters are not shown in the example below; see the widget demo. | |
360 | .PP | |
361 | If you want to group some items within a window that have a different | |
362 | packing order than others, you can include them in a Frame. This is a | |
363 | do-nothing window type that is meant for packing (and to play games | |
364 | with borders and colors). | |
365 | .PP | |
366 | The example below shows the use of pack and frames: | |
367 | .PP | |
368 | .Vb 1 | |
369 | \& #! /usr/bin/perl5 -w | |
370 | .Ve | |
371 | .PP | |
372 | .Vb 2 | |
373 | \& use strict; | |
374 | \& use Tk; | |
375 | .Ve | |
376 | .PP | |
377 | .Vb 5 | |
378 | \& # Take top, the bottom -> now implicit top is in the middle | |
379 | \& my $main = MainWindow->new; | |
380 | \& $main->Label(-text => 'At the top (default)')->pack; | |
381 | \& $main->Label(-text => 'At the bottom')->pack(-side => 'bottom'); | |
382 | \& $main->Label(-text => 'The middle remains')->pack; | |
383 | .Ve | |
384 | .PP | |
385 | .Vb 5 | |
386 | \& # Since left and right are taken, bottom will not work... | |
387 | \& my $top1 = $main->Toplevel; | |
388 | \& $top1->Label(-text => 'Left')->pack(-side => 'left'); | |
389 | \& $top1->Label(-text => 'Right')->pack(-side => 'right'); | |
390 | \& $top1->Label(-text => '?Bottom?')->pack(-side => 'bottom'); | |
391 | .Ve | |
392 | .PP | |
393 | .Vb 7 | |
394 | \& # But when you use frames, things work quite alright | |
395 | \& my $top2 = $main->Toplevel; | |
396 | \& my $frame = $top2->Frame; | |
397 | \& $frame->pack; | |
398 | \& $frame->Label(-text => 'Left2')->pack(-side => 'left'); | |
399 | \& $frame->Label(-text => 'Right2')->pack(-side => 'right'); | |
400 | \& $top2->Label(-text => 'Bottom2')->pack(-side => 'bottom'); | |
401 | .Ve | |
402 | .PP | |
403 | .Vb 1 | |
404 | \& MainLoop; | |
405 | .Ve | |
406 | .SH "More than one window" | |
407 | .IX Header "More than one window" | |
408 | Most real applications require more than one window. As you read before, | |
409 | you can create more outermost windows by using Toplevel. Each window | |
410 | is independent; destroying a toplevel window does not affect the others as | |
411 | long as they are not a child of the closed toplevel. | |
412 | Exiting the main window will end the application. | |
413 | The example below shows a trivial three-window application: | |
414 | .PP | |
415 | .Vb 1 | |
416 | \& #! /usr/bin/perl5 -w | |
417 | .Ve | |
418 | .PP | |
419 | .Vb 2 | |
420 | \& use strict; | |
421 | \& use Tk; | |
422 | .Ve | |
423 | .PP | |
424 | .Vb 7 | |
425 | \& my $main = MainWindow->new; | |
426 | \& fill_window($main, 'Main'); | |
427 | \& my $top1 = $main->Toplevel; | |
428 | \& fill_window($top1, 'First top-level'); | |
429 | \& my $top2 = $main->Toplevel; | |
430 | \& fill_window($top2, 'Second top-level'); | |
431 | \& MainLoop; | |
432 | .Ve | |
433 | .PP | |
434 | .Vb 10 | |
435 | \& sub fill_window { | |
436 | \& my ($window, $header) = @_; | |
437 | \& $window->Label(-text => $header)->pack; | |
438 | \& $window->Button(-text => 'close', | |
439 | \& -command => [$window => 'destroy'] | |
440 | \& )->pack(-side => 'left'); | |
441 | \& $window->Button(-text => 'exit', | |
442 | \& -command => [$main => 'destroy'] | |
443 | \& )->pack(-side => 'right'); | |
444 | \& } | |
445 | .Ve | |
446 | .SH "More callbacks" | |
447 | .IX Header "More callbacks" | |
448 | So far, all callback routines shown called a user procedure. | |
449 | You can also have a callback routine call another Tk routine. | |
450 | This is the way that scroll bars are implemented: scroll-bars | |
451 | can call a Tk item or a user procedure, whenever their position | |
452 | has changed. The Tk item that has a scrollbar attached calls the | |
453 | scrollbar when its size or offset has changed. In this way, | |
454 | the items are linked. You can still ask a scrollbar's position, | |
455 | or set it by hand \- but the defaults will be taken care of. | |
456 | .PP | |
457 | The example below shows a listbox with a scroll bar. Moving | |
458 | the scrollbar moves the listbox. Scanning a listbox (dragging | |
459 | an item with the left mouse button) moves the scrollbar. | |
460 | .PP | |
461 | .Vb 1 | |
462 | \& #! /usr/bin/perl5 -w | |
463 | .Ve | |
464 | .PP | |
465 | .Vb 2 | |
466 | \& use strict; | |
467 | \& use Tk; | |
468 | .Ve | |
469 | .PP | |
470 | .Vb 14 | |
471 | \& my $main = MainWindow->new; | |
472 | \& my $box = $main->Listbox(-relief => 'sunken', | |
473 | \& -width => -1, # Shrink to fit | |
474 | \& -height => 5, | |
475 | \& -setgrid => 1); | |
476 | \& my @items = qw(One Two Three Four Five Six Seven | |
477 | \& Eight Nine Ten Eleven Twelve); | |
478 | \& foreach (@items) { | |
479 | \& $box->insert('end', $_); | |
480 | \& } | |
481 | \& my $scroll = $main->Scrollbar(-command => ['yview', $box]); | |
482 | \& $box->configure(-yscrollcommand => ['set', $scroll]); | |
483 | \& $box->pack(-side => 'left', -fill => 'both', -expand => 1); | |
484 | \& $scroll->pack(-side => 'right', -fill => 'y'); | |
485 | .Ve | |
486 | .PP | |
487 | .Vb 1 | |
488 | \& MainLoop; | |
489 | .Ve | |
490 | .SH "Canvases and tags" | |
491 | .IX Header "Canvases and tags" | |
492 | One of the most powerful window types in Tk is the Canvas window. | |
493 | In a canvas window, you can draw simple graphics and include | |
494 | other widgets. The canvas area may be larger than the visible window, | |
495 | and may then be scrolled. Any item you draw on the canvas has its own id, | |
496 | and may optionally have one or more \fItags\fR. You may refer to any | |
497 | item by its id, and may refer to any group of items by a common tag; | |
498 | you can move, delete, or change groups of items using these tags, | |
499 | and you can \fIbind\fR actions to tags. For a properly designed (often | |
500 | structured) canvas, you can specify powerful actions quite simply. | |
501 | .PP | |
502 | In the example below, actions are bound to circles (single click) | |
503 | and blue items (double\-click); obviously, this can be extended to any | |
504 | tag or group of tags. | |
505 | .PP | |
506 | .Vb 1 | |
507 | \& #! /usr/bin/perl5 -w | |
508 | .Ve | |
509 | .PP | |
510 | .Vb 2 | |
511 | \& use strict; | |
512 | \& use Tk; | |
513 | .Ve | |
514 | .PP | |
515 | .Vb 4 | |
516 | \& # Create main window and canvas | |
517 | \& my $main = MainWindow->new; | |
518 | \& my $canvas = $main->Canvas; | |
519 | \& $canvas->pack(-expand => 1, -fill => 'both'); | |
520 | .Ve | |
521 | .PP | |
522 | .Vb 5 | |
523 | \& # Create various items | |
524 | \& create_item($canvas, 1, 1, 'circle', 'blue', 'Jane'); | |
525 | \& create_item($canvas, 4, 4, 'circle', 'red', 'Peter'); | |
526 | \& create_item($canvas, 4, 1, 'square', 'blue', 'James'); | |
527 | \& create_item($canvas, 1, 4, 'square', 'red', 'Patricia'); | |
528 | .Ve | |
529 | .PP | |
530 | .Vb 5 | |
531 | \& # Single-clicking with left on a 'circle' item invokes a procedure | |
532 | \& $canvas->bind('circle', '<1>' => sub {handle_circle($canvas)}); | |
533 | \& # Double-clicking with left on a 'blue' item invokes a procedure | |
534 | \& $canvas->bind('blue', '<Double-1>' => sub {handle_blue($canvas)}); | |
535 | \& MainLoop; | |
536 | .Ve | |
537 | .PP | |
538 | .Vb 3 | |
539 | \& # Create an item; use parameters as tags (this is not a default!) | |
540 | \& sub create_item { | |
541 | \& my ($can, $x, $y, $form, $color, $name) = @_; | |
542 | .Ve | |
543 | .PP | |
544 | .Vb 10 | |
545 | \& my $x2 = $x + 1; | |
546 | \& my $y2 = $y + 1; | |
547 | \& my $kind; | |
548 | \& $kind = 'oval' if ($form eq 'circle'); | |
549 | \& $kind = 'rectangle' if ($form eq 'square'); | |
550 | \& $can->create(($kind, "$x" . 'c', "$y" . 'c', | |
551 | \& "$x2" . 'c', "$y2" . 'c'), | |
552 | \& -tags => [$form, $color, $name], | |
553 | \& -fill => $color); | |
554 | \& } | |
555 | .Ve | |
556 | .PP | |
557 | .Vb 16 | |
558 | \& # This gets the real name (not current, blue/red, square/circle) | |
559 | \& # Note: you'll want to return a list in realistic situations... | |
560 | \& sub get_name { | |
561 | \& my ($can) = @_; | |
562 | \& my $item = $can->find('withtag', 'current'); | |
563 | \& my @taglist = $can->gettags($item); | |
564 | \& my $name; | |
565 | \& foreach (@taglist) { | |
566 | \& next if ($_ eq 'current'); | |
567 | \& next if ($_ eq 'red' or $_ eq 'blue'); | |
568 | \& next if ($_ eq 'square' or $_ eq 'circle'); | |
569 | \& $name = $_; | |
570 | \& last; | |
571 | \& } | |
572 | \& return $name; | |
573 | \& } | |
574 | .Ve | |
575 | .PP | |
576 | .Vb 5 | |
577 | \& sub handle_circle { | |
578 | \& my ($can) = @_; | |
579 | \& my $name = get_name($can); | |
580 | \& print "Action on circle $name...\en"; | |
581 | \& } | |
582 | .Ve | |
583 | .PP | |
584 | .Vb 5 | |
585 | \& sub handle_blue { | |
586 | \& my ($can) = @_; | |
587 | \& my $name = get_name($can); | |
588 | \& print "Action on blue item $name...\en"; | |
589 | \& } | |
590 | .Ve |