Hello World en Français

alain Adelmar
aadelmar@numericable.fr beuuh c est quoi ca

Ce tutorial est de Bin-Co, je l'ai juste traduis et pour pas le garder que pour moi je le met en ligne. Ce document est donc non officiel.


Previous
Hello World 
Next
Frame, Text, Scrollbar, Scale 
Perl/Tk Tutorial - Create GUI with Perl's Tk Module
Button, Entry, Label

Widgets 1 : Button, Entry, Label

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();

Three things need to be said about widgets. First is the widget variable. This I have explained earlier. The widget variable of all widgets must be unique and will be used whenever that widget needs to be accessed. Second is the 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. The final thing is commands. Each widget has some commands which also can be used to configure it or make it do some thing.

But 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

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

# 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 - to see why, go to the Most common mistakes by Perl/Tk beginners.


Entry

An entry is a widget that displays a one-line 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 $name = $ent -> get();
$widget -> delete(FIRST?,LAST?); Delete one or more elements of the entry. FIRST is the index of the first character to delete, and LAST is the index of the character just after the last one to delete. If last isn't specified it defaults to FIRST+1, i.e. a single character is deleted. This command returns an empty string. $ent -> delete(0,'end');
$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 $ent -> insert('end',"Hello");

Example

#!/usr/local/bin/perl
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 {
$ent -> insert('end',"Hello");
}

Label

This widget display text messages.

Some Options
-text => "TEXT" TEXT will be the text displayed on the button
-font => FONT Specifies the font to use when drawing text inside the widget. You can specify just the font or you can give it in this format "FONTNAME SIZE STYLE". The STYLE can be bold, normal etc.

Example

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

my $mw = new MainWindow; # Main Window

my $lab = $mw -> Label(-text=>"Enter name:") -> pack();
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 {
$ent -> insert(0,"Hello, ");
}
Previous
Hello World 
Next
Frame, Text, Scrollbar, Scale 
Contents

Comments

Anonymous at 31 Jul, 2007 02:49
The example above with the function push_button is not showing the text "whatever". Also if i use the command use strict,how do i write the bare text whatever
Reply to this.
Binny V A at 01 Aug, 2007 06:19
No - you misunderstood me. That 'whatever' is not the output - I meant you can put the code you want in its place. On hindsight, I think I should have commented it.
Reply to this.
Anonymous at 31 Jul, 2007 04:31
I think the function body(sub) should be written inside MainLoop itself. I had posted the same question and I think the answer is this description.
Reply to this.
Jack at 01 Aug, 2007 03:17
Is there a command to toggle the visibility of a widget? preferably label and entry widgets
Reply to this.
Binny V A at 01 Aug, 2007 06:28
There is the destroy() function that will delete a widget.

my $lab = $mw -> Label(-text=>"Enter name:") -> pack();
$lab->destroy();
Reply to this.
ANGIE at 08 Sep, 2007 04:52
YOUR CODE IS SO GREAT. YOU SHOULD START A PAID CERTIFICATION COURSE.
YOU ROCK , MAN!
Reply to this.
Anonymous at 21 Sep, 2007 11:38
As they say in Shakespeare's time, "You da Man Home-Slice"! Thanks for this!
Reply to this.
Anonymous at 21 Sep, 2007 11:43
Do you have an example of a button that the "-state" changes from "disabled" to "normal" after some event?
The following didn't work for me.

my $Enable_button="disable";
......

$mw->-> Button(-text => "Push Me",
-command =>\&push_button);
-state => $Enable_button,
or
-state => \$Enable_button,
$but -> pack();
.......
then, later
$Enable_button = "normal";

Reply to this.
Binny V A at 22 Sep, 2007 01:15
The right way to do this is...
my $button = $wm-> Button(-text => "Push Me", -command =>\&push_button, -state=>"normal");

Later
$button->configure(-state=>'disabled');
Reply to this.
Paul R at 24 Sep, 2007 07:11
Binny VA.
That's it. Thanks for your help
Reply to this.
Sibu.N.L at 11 Mar, 2008 12:59
Binny,

When I run a perl/TK simple hello world application by double clicking on it
under windows xp,
a command window also opened along with the hello world application window.
How to avoid or hide the command window from appearing on screen

Thanks in advance,

Sibu.N.L
Reply to this.
Binny V A at 11 Mar, 2008 03:53
I don't think you can do anything about that.
Reply to this.
Kerbiquet at 18 Mar, 2008 04:07
Hi,

Check this example, taken from the Perl Cookbook edited by O'Reilly: www.oreilly.com/catalog/perlckbk2/toc.html

The windows console pops up by diseappear immediatly.
Reply to this.
Kerbiquet at 18 Mar, 2008 06:33

You have written a Perl program for the Windows port of Perl and Tk, but you get a DOS shell window every time you start your program.
Add this to the start of your program:

BEGIN {
if ($^O eq 'MSWin32') {
require Win32::Console;
Win32::Console::Free( );
}
}

The Win32::Console module lets you control the terminal window that launched your program. All you need to do is close that window (or Free it in, in the peculiar parlance of the Windows API) and voila no pesky DOS shell window.
The documentation for the Win32::Console module, which is included with distributions of Perl destined for Microsoft systems
Reply to this.
Sibunl at 08 Nov, 2008 12:00
Thanks a lot.

I tried and success.

Sorry, I am so late to see this answer.

Sibu.
Reply to this.
Alex De Lara at 05 Jan, 2009 09:21
Ah, that is indeed, a nice addition to my bag of stolen tricks :)
Thank you.
Reply to this.
Anonymous at 20 Mar, 2008 09:21
Entry() takes a 1-line input..wat to do if i want t to capture a text area as input & manipulaate it..?
Reply to this.
Binny V A at 21 Mar, 2008 03:59
Its in the next page - Text widget.
Reply to this.
Anonymous at 06 May, 2008 01:29
can you please tell me how can i delete or make the scrolledlistbox invisible in the main window?
I am developing 1 application in TK(VTcl)
Reply to this.
Anonymous at 09 May, 2008 02:37
Hi Binny,
I have a sub mainp(), which runs in loop and may take less than a second or few minutes. sometimes, I want to exit from the program before mainp() finishes. I have the following code. could you please suggest me how to achieve this?

my $print = $right1->Button(-text => 'Build Index',
-command => \&mainp)->
grid(qw/-row 7 -column 1 -sticky se/);
my $exit = $right1->Button(-text => ' Exit ',
-command => [$mw => 'destroy'])->
grid(qw/-row 7 -column 2 -sticky sw/);
Reply to this.
Anonymous at 19 May, 2008 01:02
Hi,

I am trying to create and delete widgets label & entry dynamically. I am able to create widgets but unable to delete the specified range of widgets. I am using Grid manager. Could you please help me out for the same?
Reply to this.
Anonymous at 12 Jun, 2008 09:11
If you execute the perl script with wperl.exe instead of perl, no command console will be displayed. If you edit the actions for the .pl type you can make a Run with wperl.
Reply to this.
Anonymous at 02 Aug, 2008 06:41
Hi
Is it possible to make the button as hyperlink button meaning that when I click this button it open web page?
I can insert in the sub some code to open application (for ex.iexplore) but it will depend on the path to application. Is there way to get the button widget to open default web browser for any custome configuration? The only input parameter would be url of web page.
Thanks.
Reply to this.
Dennis at 13 Oct, 2008 06:06
Dear Sir :
If I turn overrideredirect to 1, then I cannot key-in on Entry square .
All key-in will appear on console cmdtool window .
Could you help me to review this code, if I make some mistake for the syntax ??
Thanks so mush for your instruction .

Attached please find the test code : (on Solaris system, SUN blade-100 computer)
use Tk;
$main = MainWindow->new;
$main->overrideredirect(1);
$frm1 = $main->Frame ( -relief => 'groove')
->pack( -side => 'top', -fill => 'both');
$lb1 = $frm1->Label ( -bg => 'white')
->pack( -side => 'left' );
$en1 = $frm1->Entry ( -relief => 'sunken', -width => 16 )
->pack( -side => 'left');
MainLoop;

Reply to this.
Anonymous at 27 Nov, 2008 02:12
hi

could you please let me know how to disable the appearance of close(X) option on a window using perl??

Thanks.
Reply to this.
Anonymous at 02 Feb, 2009 07:33
my $tton = $fileSelectFrame->Button(
-relief => "groove",
-text => "deletess ",
-command => sub {$list -> delete ("0","end");})->pack();

I have a frame which consits of buttons that are created dynamically but whilst trying to use a button to delete the contents of this frame an error: Failed to Autoload Tk::Widget::delete The $list refers to the frame
Reply to this.
Anonymous at 04 Jun, 2009 02:03
hi,
how to return the output of a subprogram to a textbox?
Reply to this.
Anonymous at 04 Jun, 2009 02:09
I am using a perl program for accessing MySQL database. The program doing some process on the retrieved data.I want to implement GUI or some web based screen for this. I need to display results of the program in textboxes and textarea. Please give me an idea about how to pass the result of the perl program to textbox or textarea.
Reply to this.
homer at 18 Aug, 2009 10:35
Hi,
I have written following program with Optionmenu and entry widget. Problem is why does entry widget does not get updated when I change the option in Optionmenu and is always stuck to "$param_text" value. And if I do any changes in entry widget then the respective parameters($param_text or $param_text1 ) should sense the changed value.

use strict;
use Data::Dumper;
use Tk;

use vars qw($textvar $var);

my $mw = MainWindow->new;
my $param_text = 5;
my $param_text1 = 6;

my $SASPhy0 = $mw -> Optionmenu(-options =>[[MinLinkRate=> \$param_text],[MaxLinkRate=> \$param_text1]], -textvariable => \$textvar, -variable => \$var)->pack();
my $ent_SASPhy0 = $mw->Entry(-textvariable=> \$$var)->pack();

MainLoop;

print $param_text;
print $param_text1;
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.