Previous
Frame, Text, Scrollbar, Scale 
Next
Listbox 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Radiobutton, Checkbutton

Widgets 3 : Radiobutton, Checkbutton

Radiobutton

Radiobutton is an input where any one of many choices MUST be chosen. If one is chosen and another button is clicked, the last chosen will lose its state and the clicked button will be chosen. A graphic example(in HTML) is given below.
Choices 1 | 2 | 3

Some Options
-command=>COMMAND Specifies a command to associate with the button. This command is typically invoked when mouse button 1 is released over the button window.
-variable => \$VARIABLE Specifies name of global variable to set to indicate whether or not this button is selected.
-value => VALUE Specifies value to store in the button's associated variable whenever this button is selected.


SyntaxDescriptionExample
$widget -> deselect(); Deselects the checkbutton and sets the associated variable to its "off" value. $rdb_m -> deselect();
$widget -> select() Selects the checkbutton and sets the associated variable to its "on" value. $rdb_m -> select();


Example

#!/usr/local/bin/perl
use Tk;

#Global Variables
my $age = 10;
my $gender = "Male";

# Main Window
my $mw = new MainWindow;

#GUI Building Area
my $frm_name = $mw -> Frame();
my $lab = $frm_name -> Label(-text=>"Name:");
my $ent = $frm_name -> Entry();
#Age
my $scl = $mw -> Scale(-label=>"Age :",
	 -orient=>'v',	 	-digit=>1,
	 -from=>10,		-to=>50,
	 -variable=>\$age,	-tickinterval=>10);

#Gender
my $frm_gender = $mw -> Frame();
my $lbl_gender = $frm_gender -> Label(-text=>"Sex ");
my $rdb_m = $frm_gender -> Radiobutton(-text=>"Male",  
		-value=>"Male",  -variable=>\$gender);
my $rdb_f = $frm_gender -> Radiobutton(-text=>"Female",
		-value=>"Female",-variable=>\$gender);


my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button);

#Text Area
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]);

#Geometry Management
$lab -> grid(-row=>1,-column=>1);
$ent -> grid(-row=>1,-column=>2);
$scl -> grid(-row=>2,-column=>1);
$frm_name -> grid(-row=>1,-column=>1,-columnspan=>2);

$lbl_gender -> grid(-row=>1,-column=>1);
$rdb_m -> grid(-row=>1,-column=>2);
$rdb_f -> grid(-row=>1,-column=>3);
$frm_gender -> grid(-row=>3,-column=>1,-columnspan=>2);

$but -> grid(-row=>4,-column=>1,-columnspan=>2);

$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=>5,-column=>1,-columnspan=>2);

MainLoop;

## Functions
#This function will be executed when the button is pushed
sub push_button {
	my $name = $ent -> get();
	$txt -> insert('end',"$name\($gender\) is $age years old.");
}

This time the program is subjected to even more change - the geometry manager is fully grid now. There is no instances of pack. You will find this necessary when the layout becomes more complicated. I hope you can stay with me in such trying times.


Checkbutton

Checkbotton is a input with two options - Off or On - it has to be either one. The state can be changed by clicking on it. An example is shown below.
check box

Some Options
-offvalue=>VALUE Specifies value to store in the button's associated variable whenever this button is deselected. Defaults to ``0''.
-onvalue=>VALUE Specifies value to store in the button's associated variable whenever this button is selected. Defaults to ``1''.
-command=>CALLBACK Specifies a command to associate with the button. This command is typically invoked when mouse button 1 is released over the button window.
-variable=>\$VARABLE Specifies name of global variable to set to indicate whether or not this button is selected.


SyntaxDescriptionExample
$widget -> deselect(); Deselects the checkbutton and sets the associated variable to its ``off'' value. $chk -> deselect();
$widget -> select(); Selects the checkbutton and sets the associated variable to its ``on'' value. $chk -> select();
$widget -> toggle(); Toggles the selection state of the button, redisplaying it and modifying its associated variable to reflect the new state. $chk -> toggle();


Example

#!/usr/local/bin/perl
use Tk;

#Global Variables
my $age = 10;
my $gender = "Male";
my $occupied = 1;

# Main Window
my $mw = new MainWindow;

#GUI Building Area
my $frm_name = $mw -> Frame();
my $lab = $frm_name -> Label(-text=>"Name:");
my $ent = $frm_name -> Entry();
#Age
my $scl = $mw -> Scale(-label=>"Age :",
	 -orient=>'v',	 	-digit=>1,
	 -from=>10,		-to=>50,
	 -variable=>\$age,	-tickinterval=>10);
	
#Jobs
my $chk = $mw -> Checkbutton(-text=>"Occupied",
	-variable=>\$occupied);
$chk -> deselect();

#Gender
my $frm_gender = $mw -> Frame();
my $lbl_gender = $frm_gender -> Label(-text=>"Sex ");
my $rdb_m = $frm_gender -> Radiobutton(-text=>"Male",  
		-value=>"Male",  -variable=>\$gender);
my $rdb_f = $frm_gender -> Radiobutton(-text=>"Female",
		-value=>"Female",-variable=>\$gender);

my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button);

#Text Area
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]);

#Geometry Management
$lab -> grid(-row=>1,-column=>1);
$ent -> grid(-row=>1,-column=>2);
$frm_name -> grid(-row=>1,-column=>1,-columnspan=>2);

$scl -> grid(-row=>2,-column=>1);
$chk -> grid(-row=>2,-column=>2,-sticky=>'w');

$lbl_gender -> grid(-row=>1,-column=>1);
$rdb_m -> grid(-row=>1,-column=>2);
$rdb_f -> grid(-row=>1,-column=>3);
$frm_gender -> grid(-row=>3,-column=>1,-columnspan=>2);

$but -> grid(-row=>4,-column=>1,-columnspan=>2);

$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=>5,-column=>1,-columnspan=>2);

MainLoop;

## Functions
#This function will be executed when the button is pushed
sub push_button {
	my $name = $ent -> get();
	$txt -> insert('end',"$name\($gender\) is $age years old.");
}
Previous
Frame, Text, Scrollbar, Scale 
Next
Listbox 
blog comments powered by Disqus
Subscribe to Feed