Tk Basics

To use the Tk module, you need to know some basics first. This chapter starts with an impractical example, the Tk version of “Hello World” (see Listing 13.1).

Listing 13.1. hello.pl
use strict; 
use warnings; 

use Tk; 

sub byebye() 
{  
    exit (0); 
} 

my $tk_mw = MainWindow–>new(); 
my $tk_button = $tk_mw–>Button(
    –text => "Hello World", 
    –command => \&byebye 
); 

$tk_button–>pack(
    –side => 'top' 
); 

MainLoop();

When run, this program displays a window containing a single button labeled “Hello” as seen in Figure 13.1. When this button is clicked, the program exits.

Figure 13.1. Hello World program.

Three important graphical elements are involved ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.