NAME
Tkx::Tutorial - How to use Tkx
DESCRIPTION
Tk is a toolkit that allows you to create applications with graphical interfaces for Windows, Mac OS X and X11. The Tk
toolkit is native to the Tcl programming language, but its ease of use and cross-platform availabilty has made it the GUI toolkit of choice for many other dynamic languages as well.
Tkx
is a Perl module that make the Tk toolkit available to Perl programs. By loading the Tkx
module Perl programs can create windows and fill them with text, images, buttons and other controls that make up the user interface of the application.
Hello World
Let's start with the mandatory exercise of creating an application that greats the world. Here we make the application window contain a single button which will shut down the application if clicked. The code to make this happen is:
use Tkx;
Tkx::button(".b",
-text => "Hello, world",
-command => sub { Tkx::destroy("."); },
);
Tkx::pack(".b");
Tkx::MainLoop()
Save this to a file called hello.pl and then run perl hello.pl
to start up the application. A window with the text "Hello, world" should appear on your screen.
After the Tkx
module has been loaded by the use Tkx;
statement the application will show an empty window called ".". We create a button with the name ".b" and tell the window to display the button with the call to Tkx::pack()
. After the layout of the window has been set up we need to pass control back to Tk so that it can draw the window and invoke our callback if the button is clicked. This is achieved by the Tkx::MainLoop()
call at the end. Clicking the button will invoke the subroutine registered with the -command
option of the button. In this case the callback simply destroys the window, which in turn will terminate the application.
For reference this is how the same program would look in Tcl:
package require Tk
button .b \
-text "Hello, world" \
-command { destroy . }
pack .b
This program can be executed by the tclsh binary that comes with Tcl/Tk. As you can see the code is mostly identical, but with a slightly different syntax. The only real difference is that the call to MainLoop() is implicit in Tcl and does not have to be spelled out.
Tkx
does not come with documentation that explain all the widgets (like "button" above) that are available for use. Instead you will need to read the excellent documentation that comes with Tcl/Tk
and then figure out how this translates to Tkx
yourself. As you can see this translation is straight forward. You basically only have add the prefix "Tkx::" to all the functions and use the Perl way of passing arguments. Tcl is a very simple language, so it should not take long to understand enough of it to be able to read its documentation with ease.
A great place to look up the Tk documentation is http://aspn.activestate.com/ASPN/docs/ActiveTcl/at.pkg_index.html. This documents core Tk as well as all the useful add-on packages that are part of ActiveTcl. The ActiveTcl HTML documentation can also be downloaded from http://downloads.activestate.com/ActiveTcl/html/ and installed locally. The official Tcl/Tk docs are found at http://www.tcl.tk/doc/.
Hello World with objects
The windows and controls that make up a Tk
interface are called widgets. The widgets are identified by path names of the form .foo.bar.baz
. These names are hierarchical in the same way as file system names are, but "." is used instead of "/" to separate levels. The name .foo.bar.baz
is the name of a widget that is child of widget .foo.bar
which in turn is a child of .foo
. At the top of this hierarchy we have a widget called .
, which is the main window of the application.
The Tkx
module provide a class called Tkx::widget
, that can be used to hide the details of Tk path names from Tkx
applications. This provide a more "perlish" way to create and manipulate Tk widgets.
Our "Hello, world" program can be rewitten like this using the Tkx::widget
class:
use Tkx;
my $mw = Tkx::widget->new(".");
my $b = $mw->new_button(
-text => "Hello, world",
-command => sub { $mw->g_destroy; },
);
$b->g_pack;
Tkx::MainLoop()
By loading the Tkx
module we make the Tkx::widget
class available and create the main window (the widget called .
). Next we instantiate a new Tkx::widget
object wrapping the main window. It is customary to name this object $mw
.
To create a new button child widget we call $mw->new_button
method. Constructor methods are always prefixed with new_
. The rest of the method name is the name of the Tk widget to create; i.e. "button" in this case. Arguments are passed as before.
Calling a "g_" method will invoke the correspoding Tk command with the widget path as argument. In the code above we destroy the main window by calling $mw->g_destroy
and we pack the button in the main window by invoking $b->g_pack
.
Calling a "m_" method will invoke a Tk subcommand for the given widget, but there is no example of this is the code above. The most commonly used subcommand is "configure" that is used to change the attributes of a widget. We could for instace change the text displayed by the button by invoking:
$b->m_configure(-text => "Goodbye, cruel world");
In the end we need to invoke the MainLoop as before.
For trivial programs like the one above using Tkx::widget
wrappers does not appear to be a win, but as the application grows and the Tk path names get longer it surely is.
Setting up a menu line
Not written yet.
Using Tcl packages
Not written yet.
Setting up application icons
Not written yet.
Subclassing Tkx::widget
Not written yet.
Mega widgets
Not written yet.
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Copyright 2005 ActiveState. All rights reserved.