For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > November 2004 > how to access the variable in perl tk









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to access the variable in perl tk
Jie Huang

2004-11-15, 3:58 pm

I am trying to use the following piece of code to do a simple
calucation on the input data.
For example, if the user inputs a number 2, and then press the
"Calculate" button, the number 4 should be displayed. Of course, my
code didn't work that way. Here are questions:
1. how to get the correct ouput, that is, input times 2?
2. Each time when I press the "Calculate" button, a new output line is
generated. how to avoid that?
3. how to add a button to Save the whole content or some variable
value into a file?

thanks very much.

Jie


===my perl code=====
$input_data = $main -> Entry(-relief=>'sunken')->pack();
$Calculate = $main -> Button(-text=>'Calculate',
-command=>\&compute)->pack();
sub compute() {
$output_data = $main -> Label(-text=>'$input_data * 2')->pack;
}
Ingo Strauch

2004-11-16, 8:56 am

On 15 Nov 2004 08:55:34 -0800, Jie Huang wrote:
> I am trying to use the following piece of code to do a simple
> calucation on the input data.
> For example, if the user inputs a number 2, and then press the
> "Calculate" button, the number 4 should be displayed. Of course, my
> code didn't work that way.


Hm, what happened instead?


> Here are questions:
> 1. how to get the correct ouput, that is, input times 2?


I think it has to be $input_data * 2 instead of '$input_data * 2'. When
you put it in ticks, the entire thing is interpreted as a string. For
single ticks as you used them not even the variable gets replaced by its
content.


> 2. Each time when I press the "Calculate" button, a new output line is
> generated. how to avoid that?


Before your subroutine add a line

$output_data = $main -> Label()->pack

and in compute() use something like

$output_data->SetText($input_data * 2);

You need to look up in the documentation how to set the text of a label
widget.


> 3. how to add a button to Save the whole content or some variable
> value into a file?


Very short skeleton

open(FILE,">filename.txt") or die "Couldn't open filename.txt for writing";
print FILE, $variable;
close(FILE);

That only works, if $variable is a string or number. If you want to
store complex data structures you could have a look at the Storable
module.


>===my perl code=====
> $input_data = $main -> Entry(-relief=>'sunken')->pack();
> $Calculate = $main -> Button(-text=>'Calculate',
> -command=>\&compute)->pack();
> sub compute() {
> $output_data = $main -> Label(-text=>'$input_data * 2')->pack;
> }


HTH,
Ingo

--
Ingo Strauch ---- Registered Linux User #227900 (http://counter.li.org/)
http://www.the-one-brack.org/

zentara

2004-11-16, 8:56 am

On 15 Nov 2004 08:55:34 -0800, jiehuang001@hotmail.com (Jie Huang)
wrote:

>I am trying to use the following piece of code to do a simple
>calucation on the input data.
>For example, if the user inputs a number 2, and then press the
>"Calculate" button, the number 4 should be displayed. Of course, my
>code didn't work that way. Here are questions:
>1. how to get the correct ouput, that is, input times 2?
>2. Each time when I press the "Calculate" button, a new output line is
>generated. how to avoid that?
>3. how to add a button to Save the whole content or some variable
>value into a file?


Here are a couple of examples. The first uses -textvariable instead of
-text, since it updates automatically. The second way is included to
show how to do it with -text.

By the way, if you go to http://learn.perl.org
and subscribe to the perl.beginners maillist, you
can get alot of simple questions answered.

#!/usr/bin/perl
use warnings;
use strict;
use Tk;

my $main = tkinit;

my $input1 = 0;
my $input_ent1 = $main->Entry(-relief=>'sunken',
-textvariable=> \$input1,
)->pack();

my $input2 = 0;
my $input_ent2 = $main->Entry(-relief=>'sunken',
-textvariable=> \$input2,
)->pack();

my $output = 0;
my $output_lab = $main->Label(-textvariable => \$output,
-bg =>'black',
-fg =>'red')->pack;

my $Calculate = $main->Button(-text=>'Calculate',
-command=>\&compute)->pack();

my $save_but = $main->Button(-text => 'Save',
-command => \&save_it
)->pack;

my $exit_but = $main->Button(-text => 'Exit',
-command => sub { exit }
)->pack;

$input_ent1->focus; #puts cursor in first entry box

MainLoop;

########################################
########################
sub compute() {
$output = $input1 * $input2;
}
########################################
#######################

sub save_it {
open( MYOUT, ">> $0.output" ) or die "$!\n";
print MYOUT "input1->$input1 times input2->$input2 = $output\n";

}
__END__



SECOND EXAMPLE:
#!/usr/bin/perl
use warnings;
use strict;
use Tk;

my $main = tkinit;

my $input_ent1 = $main->Entry(-relief=>'sunken' )->pack();

my $input_ent2 = $main->Entry(-relief=>'sunken' )->pack();

my $output_lab = $main->Label(-text => ' ',
-bg =>'black',
-fg =>'red')->pack;

my $Calculate = $main->Button(-text=>'Calculate',
-command=>\&compute)->pack();

$input_ent1->focus; #puts cursor in first entry box

MainLoop;

########################################
########################
sub compute() {
my $input1 = $input_ent1->get();
my $input2 = $input_ent2->get();
my $output = $input1 * $input2;
$output_lab->configure(-text => $output);

}
__END__

--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com