Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Perl::Tk Is it possible to configure "anonymous" widgets?

by Microcebus (Beadle)
on Oct 14, 2010 at 12:55 UTC ( [id://865250]=perlquestion: print w/replies, xml ) Need Help??

Microcebus has asked for the wisdom of the Perl Monks concerning the following question:

First Thanks for the quick help to my last question but this led me to another question:

If I have a code like:
#! /usr/bin/perl -w use Tk; $mw=MainWindow->new(); foreach(1..10) { $mw->Entry()->pack; } MainLoop;

Is there a possibility to configure a certain widget after it is generated without using a specific variable? And can I get the input, lets say from the fith Entry-widget?

Replies are listed 'Best First'.
Re: Perl::Tk Is it possible to configure "anonymous" widgets?
by zentara (Archbishop) on Oct 14, 2010 at 14:37 UTC
    Doing things anonymously just brings alot of confusion, but it can be done. You can always get the widget's internal identifier with $widget->id;
    #! /usr/bin/perl -w use warnings; use strict; use Tk; my $mw=MainWindow->new(); foreach(1..10) { my $e = $mw->Entry()->pack; $e->bind('<FocusIn>', sub { print "@_\n"; print "my entry is $_[0]\n"; $_[0]->configure(-bg => 'hotpink'); } ); } MainLoop;
    but you are far better off storing everthing in hashes, because you can then get access to each entry from anywhere in the program.
    #! /usr/bin/perl -w use warnings; use strict; use Tk; my $mw=MainWindow->new(); my %entries; foreach(1..10) { $entries{$_}{'widget'} = $mw->Entry()->pack; $entries{$_}{'widget'}->bind('<FocusIn>', sub { $_[0]->configure(-bg => 'hotpink'); }); } $mw->after(5000,sub{ foreach(2,4,6,8){ $entries{$_}{'widget'}->configure(-bg => 'hotpink'); } }); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Perl::Tk Is it possible to configure "anonymous" widgets?
by kcott (Archbishop) on Oct 14, 2010 at 13:10 UTC

    Assuming you are talking about the configure() method, the answer is no. Consider:

    $widget->configure(option => 'value');

    It's $widget invoking the configure() method. Without $widget, Tk wouldn't know what to configure.

    "And can I get the input, lets say from the fith Entry-widget?"

    Not using the code you have there. You'd need to store them in an array then access $entry_widgets[4].

    -- Ken

Re: Perl::Tk Is it possible to configure "anonymous" widgets?
by zentara (Archbishop) on Oct 14, 2010 at 14:45 UTC
    There is also the packSlaves method, but you won't know for sure which entry is which, although I would ASSUME they are listed in creation order.
    #! /usr/bin/perl -w use Tk; $mw=MainWindow->new(); foreach(1..10) { $mw->Entry()->pack; } $mw->after(100,sub{ print $mw->packSlaves,"\n"; }); MainLoop;
    P.S. There is also the Tk::caller method, but it requires a bind.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://865250]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-23 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found