Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

how to access the variable in perl tk
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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
Jie Huang
11-15-04 08:58 PM


Re: how to access the variable in perl tk
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/


Report this thread to moderator Post Follow-up to this message
Old Post
Ingo Strauch
11-16-04 01:56 PM


Re: how to access the variable in perl tk
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

Report this thread to moderator Post Follow-up to this message
Old Post
zentara
11-16-04 01:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PerlTk archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:12 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.