Click here to get back home

Perl Tk:Scheduler command called subroutine fails to talk to tk widgets

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Perl Tk:Scheduler command called subroutine fails to talk to tk widgets Andy 01-31-2007
Get Chitika Premium
Posted by Dominique Dumont on February 2, 2007, 4:35 am
Please log in for more thread options



> Does anyone know why? Do I have to call some sort of "refresh" tk
> command or release tk resources?

Have you tried with an anonymous sub ? This sub is stored inside a
lexical variable so there's no problem with namespace.

I.e. (untested) :

use Tk;
use Tk::Schedule;

my $mw = MainWindow->new;

my $txtbox ; # variable declared before anon sub creation

my $run = sub {
print "start run\n";
$txtbox->insert(end,"this just hangs\n");
print "exit run\n";
} ;

my $s = $mw->Schedule(
-interval => 60,
-repeat => "once",
-command => [ $run, "junk"],
-comment => "1-9999"
)->pack();

$txtbox=$mw->Text(-width => 79,
-height => 8,
)->pack();

$txtbox->insert(end, "this works\n");

MainLoop;

# HTH

--
Dominique Dumont
"Delivering successful solutions requires giving people what they
need, not what they want." Kurt Bittner

Posted by Andy on February 2, 2007, 10:20 am
Please log in for more thread options


I've read your reply and tried moving the run subroutine into a
variable, and declaring it and the tk read only text widget before the
creation of the scheduler. The code compiles and runs. But, when the
scheduler tries to parse the code in the command variable, the
following error occurs (buried deep inside of some perl modules the
scheduler itself uses):

Tk::Error: Usage: ->coderef2text(CODEREF) at C:/Perl/site/lib/Tk/
Schedule.pm line 374
Carp::croak at C:/Perl/lib/Carp.pm line 269
B::Deparse::coderef2text at C:/Perl/lib/B/Deparse.pm line 671
Tk::Schedule::AddTime at C:/Perl/site/lib/Tk/Schedule.pm line 374
Tk callback for .schedule.frame.button
Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 252
Tk::Button::butUp at C:/Perl/site/lib/Tk/Button.pm line 111
<ButtonRelease-1>
(command bound to event)

As I haven't written the scheduler or the other modules it uses, I
have no idea what this error is saying went wrong. But, I suspect it
has to do with something with the command code being treated as plain
text before it is executed in the eval statement within the
shceduler. If so, we seem to be losing the memory references to
variables again.

But, thanks for the suggestion.


Posted by Andy on February 2, 2007, 10:26 am
Please log in for more thread options


oops I found a typo - got rid of the Tk::Error: Usage: -
>coderef2text(CODEREF) at C:/Perl/site/lib/Tk/
Schedule.pm line 374 error. But, calls to the widgets still hang.



Posted by Andy on February 2, 2007, 10:37 am
Please log in for more thread options


But, if I keep the creation of the read only text widget before the
creation of the scheduler as Dominique Dumont had suggested, and pass
the variable that holds the reference to the read only text widget to
the command as an argument to the run subroutine, and I keep the
package name for the main Perl program, the run subroutine is then
able to talk to the tk widget!

package Tk::BulkE;

use Tk;
use Tk::Schedule;

my $mw = MainWindow->new;

my $txtbox=$mw->Text(-width => 79,
-height => 8,
)->pack();

my $s = $mw->Schedule(
-interval => 60,
-repeat => "once",
-command => [\&run, $txtbox],
-comment => "1-9999"
)->pack();

$txtbox->insert(end, "this works\n");


MainLoop;


sub run{
my $txtbox=shift;

print "start run\n";
$txtbox->insert(end,"and this now works (previously it hanged)
\n");
print "exit run\n";
}


Posted by Dominique Dumont on February 5, 2007, 5:35 am
Please log in for more thread options



> But, if I keep the creation of the read only text widget before the
> creation of the scheduler as Dominique Dumont had suggested, and pass
> the variable that holds the reference to the read only text widget to
> the command as an argument to the run subroutine, and I keep the
> package name for the main Perl program, the run subroutine is then
> able to talk to the tk widget!
>
> package Tk::BulkE;
>
> use Tk;
> use Tk::Schedule;
>
> my $mw = MainWindow->new;
>
> my $txtbox=$mw->Text(-width => 79,
> -height => 8,
> )->pack();
>
> my $s = $mw->Schedule(
> -interval => 60,
> -repeat => "once",
> -command => [\&run, $txtbox],

The problem was related to $txtbox closure. Here, the $txtbox ref is
copied when you call Schedule, so you don't rely on a lexical.

HTH

--
Dominique Dumont
"Delivering successful solutions requires giving people what they
need, not what they want." Kurt Bittner

Similar ThreadsPosted
comp.lang.perl.modules,alt.autos.rod-n-custom,alt.kids-talk,alt.internet.p2p,alt.support.nutty.as.a.fruitcake December 28, 2004, 2:24 am
use of xml in perl application to describe command line logic December 20, 2005, 11:03 am
Perl/Tk build fails on HP-UX February 27, 2006, 4:58 pm
Net::LDAP makefile generation fails on ActiveState Perl 5.8.8 June 23, 2006, 2:57 am
Log::Dispatch - How to "die" a script after all other methods are called??? September 6, 2005, 4:55 pm
Curses::Widgets::Menu Question May 11, 2005, 8:32 pm
Compile problem: Curses::Widgets September 24, 2007, 2:54 pm
SOLVED: Curses::Widgets Runtime Error October 13, 2007, 3:01 pm
"Undefined subroutine" November 21, 2004, 8:48 pm
catch_int\catch_hup subroutine February 25, 2005, 8:51 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap