A widget is a user interface object in X graphical user interfaces Confused? Lets just say that it is the name of the object that appears on screen There are many types widgets If you want to display a button, you use the button widget For text, you use the text widget For entry, you guessed it, the entry widget.
Syntax:
my $WidgetVariable = $Window >WidgetType(?Option 1=>Value 1, ?Option 2=>Value 2 ??) ->pack();
widgetVariable - The widget variable of all widgets must be unique and will be used whenever that widget needs to be accessed.
options - Each widget has some options which can be used to configure it This is usually done when the widget is declared, but it can be done afterward also.
commands - Each widget has some commands which also can be used to configure it or make it do some thing.
Before we begin, we need to know a little about the pack command I have explained this earlier but just doing it one more time so that you don't have to push the back button Pack is a geometry manager Another geometry manager is 'grid' we will explore that latter Pack is much more simpler than grid The line $hello ->pack; tells the interpreter to pack the widget called "$hello".
Button:
This will make a button It can be configured to execute some code when the button is pushed This will usually refer to a function so when the button is pushed, the function will run An button is shown below This button is created using HTML input tag
Some Options:
text=>"TEXT" - TEXT will be the text displayed on the button
command=>CALLBACK - CALLBACK will be the code that is called when the
button is pushed
# Main Window
my $mw = new MainWindow;
my $but = $mw -> Button(-text => "Push Me",-command =>\&push_button);
$but -> pack();
MainLoop;
#This is executed when the button is pressed
sub push_button
{
whatever...
}
You may have noticed that I used a slash(\) in the command callback
(-command =>\&push_button);) Make sure that the slash stays there - It pass the reference to its sub function.
Entry:
An entry is a widget that displays a oneline text string and allows the user to input and edit text in it When an entry has the input focus it displays an insertion cursor to indicate where new characters will be inserted An entry element is shown using HTML
Some Options:
width=>NUMBER
Width of the input field NUMBER should be an integer.
textvariable=>\$VARIABLE
The contents of the variable VARIABLE will be displayed in the widget If the text in
the widget is edited, the variable will be edited automatically
state=>STATE
The state of the input field It can be normal, disabled, or readonly If it is readonly
the text can't be edited
Some Commands
Syntax Description Example
$widget ->get(); The text inside input field can be taken by this command.
$widget ->delete(FIRST?,LAST?); Delete one or more elements
of the entry FIRST is $ent ->
$widget ->insert(index,"STRING"); Insert the characters of STRING just before the
character indicated by index Index is 0 for the first character The word "end" can be used for the last character.
Example
use Tk;
# Main Window
my $mw = new MainWindow;
#GUI Building Area
my $ent = $mw -> Entry() -> pack();
my $but = $mw -> Button(-text => "Push Me",-command =>\&push_button);
$but -> pack();
MainLoop;
#This is executed when the button is pressed
sub push_button
sub push_button
{
$ent -> insert('end',"Hello");
}
$ent -> insert('end',"Hello");
}
No comments:
Post a Comment