Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Perl/Tk top-level widgets

Status
Not open for further replies.

Paradox04

Programmer
May 13, 2004
1
US
I'm writing this program in Perl/Tk and I need to be able to pass a top-level widget to a function. Here is an example of what I mean:

sub start {

my $mw = MainWindow->new;

my $button = $mw->Button(-text=>"Go To Foo", -command => sub {foo($mw);})->pack;
}

sub foo {

my ($parent) = @_;

my $new_window = $parent->new;
}

So, what I'm trying to do here is if a use clicks a button, it will pass the scalar containing the top-level widget to a function which will then create a new window with the top-level widget as the parent. This code obviously doesn't work. It generates this error:

Tk::Error: Can't locate object method "apply_command_line" via package "MainWindow=HASH(0x20761764)" (perhaps you forgot to load "MainWindow=HASH(0x20761764)"?) at /usr/local/pkg/perl/perl-5.6.1/lib/site_perl/5.6.1/aix/Tk/MainWindow.pm line 57.
Tk callback for .frame.button
Tk::__ANON__ at /usr/local/pkg/perl/perl-5.6.1/lib/site_perl/5.6.1/aix/Tk.pm line 225
Tk::Button::butUp at /usr/local/pkg/perl/perl-5.6.1/lib/site_perl/5.6.1/aix/Tk/Button.pm line 111
(command bound to event)

Does anyone know anything about this?
 
It's been a while since I used TK but... shouldn't $mw have more scope than that? I would have declared it outside of the subs.

And... Have you tried passing a reference to $mw like this?


my $button = $mw->Button(-text=>"Go To Foo", -command => sub {foo(\$mw);})->pack;



Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top