Previous
Menubutton, Menu 
Next
Dialogs 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Canvas, Message, Adjuster, Scrolled

Some more Widgets - Canvas, Message, Adjuster, Scrolled

Canvas

The canvas widget is a very important widget as all points are addressable graphical drawing area. Canvas widgets implement structured graphics. A canvas displays any number of items, which may be things like rectangles, circles, lines, and text. Items may be manipulated (e.g. moved or re-colored) and commands may be associated with items. So if you don't like the paint program in windows, you can make your own program using this widget.

The command $widget -> create type options is used to make different structures. A few examples are given below. For more information read the manual.

Example

#!/usr/local/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;

my $cns = $mw -> Canvas(-relief=>"sunken", -background=>"blue");
$cns -> create('polygon',5,100,50,5,150,5,200,100,5,100,
	-joinstyle=>"bevel", -fill=>"red", -outline=>"white", -width=>5);
$cns -> create('oval',200,100,300,200, -fill=>"green");
$cns -> create('oval',100,150,300,100, -fill=>"white", -width=>0);
$cns -> create('rectangle',10,150,100,250, -dash=>[6,4,2,4,2,4]);
$cns -> pack;

MainLoop;

Message

A message is a widget that displays a textual string. Much like the label widget but this can be used to make a multi-line text.

The -justify option specifies how to justify lines of text. Must be one of left, center, or right. Defaults to left. This option works together with the anchor, aspect, padX, padY, and width options to provide a variety of arrangements of the text within the window.


Adjuster

An adjuster acts like the frame widget - with one notable exception. The borders can be dragged and expended. This widget contains any number of panes, arranged horizontally or vertically, according to the value of the -orient option. Each pane contains one widget, and each pair of panes is separated by a movable sash. Moving a sash can be done by dragging it. This causes the widgets on either side of the sash to be resized.

Some Options

-side=>DIRECTIONSpecifies the side on which the managed widget lies relative to the Adjuster. In conjunction with the pack geometry manager, this relates to the side of the master against which the managed widget and the Adjuster are packed. Must be left, right, top, or bottom. Defaults to top.

Some Methods
$adjuster -> packAfter($widget, ?pack_options?) This command configures the Adjuster's -widget and -side options respectively to '$widget' and the -side value specified in pack_options (top if not specified). It then packs the Adjuster after '$widget', with -fill set to x or y as appropriate.
Example
use Tk;
use Tk::Adjuster;
my $mw = new MainWindow;

my $adj = $mw -> Adjuster();
my $lst = $mw -> Listbox();
$lst -> insert('end', "Item 1");
$lst -> insert('end', "Item 2");
$lst -> insert('end', "Item 3");
$lst -> insert('end', "Item 4");
$lst -> insert('end', "Item 5");

my $txt = $mw -> Scrolled("Text",-scrollbars=>'e');
$txt -> insert('end',"To Hack With It

     To Compute...
     Or Not To Compute...
     That Is The Question...
     Whether 'Tis Nobler In The Memory Bank..
     To Suffer The Slings And Circuits Of Outrageous Functions...
     ...Or To Take Up Arms Against A Sea Of..Transistors,
     Or Rather Transponders...
     Transcondu--
     Trans...
     Er...           Oh, To Hack With It.");


my $side = "left";
$lst -> pack(-side => $side, -fill => 'both', -expand => 1);
$adj -> packAfter($lst, -side => $side);
$txt -> pack(-side => $side, -fill => 'both', -expand => 1);

MainLoop;


Scrolled

Scrolled is a derived widget that creates a widget with attached scrollbar(s). If you have tried to create a widget and attach scrollbars to it, you will doubtlessly understand the usefulness of this feature. It greatly reduces the number of code for these situations.

my $widget = $parent -> Scrolled('WIDGET' ?,-scrollbars=>WHERE? ?,...?);

WIDGET can be any widget that supports scrolling. For example, Text, Listbox, etc.

Some Options

-scrollbars => SIDEExpects as argument the position where the scrollbars should be created: w, e or n, s or a combination of them. If the one or both positions are prefixed with o the scrollbar will only show up if there is a 'real' need to scroll.

Example:
use Tk;
my $mw = new MainWindow;

my $txt = $mw -> Scrolled('Text',-scrollbars=>"oe") -> pack;
$txt -> insert('end',
"Arthur: \"It's at times like this I wish I'd listened to my mother.\"
Ford : \"Why, what did she say?\"
Arthur: \"I don't know, I never listened.\"
		Douglas Adams");

MainLoop;
The same example using hand coded scrollbars.
use Tk;
my $mw = new MainWindow;

my $textarea = $mw -> Frame();
my $txt = $textarea -> Text(-width=>40, -height=>10);
my $srl_y = $textarea -> Scrollbar(-orient=>'v',-command=>[yview => $txt]);
my $srl_x = $textarea -> Scrollbar(-orient=>'h',-command=>[xview => $txt]);
$txt -> configure(-yscrollcommand=>['set', $srl_y],
		-xscrollcommand=>['set',$srl_x]);
$txt -> insert('end',
"Arthur: \"It's at times like this I wish I'd listened to my mother.\"
Ford : \"Why, what did she say?\"
Arthur: \"I don't know, I never listened.\"
		Douglas Adams");

#Geometry
$txt -> grid(-row=>1,-column=>1);
$srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns");
$srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew");
$textarea -> grid(-row=>1,-column=>1,-columnspan=>2);

MainLoop;

As you can see, the Scrolled widget saves quite a bit of typing.

Previous
Menubutton, Menu 
Next
Dialogs 
Subscribe to Feed