need help for my program
am 16.10.2004 01:14:07 von camel
Hello all,
I am new to perl and perl/tk. I wrote a small program to test thread and got
exception which I have no idea what is wrong with my program. Can anyone
kindly explain it to me? Below is my program.
Thanks in advance.
jh
use Tk;
use Thread;
my $main = MainWindow->new();
$main->minsize(qw(250 250));
$t = $main->Text()->pack();
$b = $main->Button(-text=>'go', -command=>\&doit)->pack();
sub doit {
$tt = Thread->new(\&display);
}
MainLoop();
sub display {
$c = 0;
while($c<10) {
$t->insert('end', "$c\n");
$c++;
sleep(1);
}
}
Re: need help for my program
am 16.10.2004 17:06:31 von Matt Garrish
"Camel" wrote in message
news:3RYbd.6071$6q2.5192@newssvr14.news.prodigy.com...
> Hello all,
>
> I am new to perl and perl/tk. I wrote a small program to test thread and
> got
> exception which I have no idea what is wrong with my program. Can anyone
> kindly explain it to me? Below is my program.
>
In the future, please post what the error message is. It can often spare
people the time of running your code (although not in this case).
Always start your scripts with:
use strict
use warnings;
And indent your code if your going to post it for help. It makes it a lot
easier to read.
> use Tk;
>
> use Thread;
>
> my $main = MainWindow->new();
>
> $main->minsize(qw(250 250));
>
> $t = $main->Text()->pack();
>
> $b = $main->Button(-text=>'go', -command=>\&doit)->pack();
Try to avoid using the variables $a and $b in your scripts (see the sort
function). One letter variables should be avoided in general, but
especially when posting for help.
>
> sub doit {
>
> $tt = Thread->new(\&display);
>
> }
>
> MainLoop();
>
> sub display {
>
> $c = 0;
>
> while($c<10) {
>
The above loop is better written as:
for my $c (0..9) {
> $t->insert('end', "$c\n");
This is line is your problem. You can't access the widgets in the main
window from a new thread, and that's what causes the program to crash. It
*might* be possible to use threads::shared to access the widgets in your
main window, but I'm no expert on Tk. If it's really important that you find
a solution using a Tk window, you can always try asking in comp.lang.perl.tk
and see if anyone there knows a way to make it work.
Matt