Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

perl tk bind callback

by zemplen (Novice)
on Oct 17, 2003 at 17:09 UTC ( #300074=perlquestion: print w/ replies, xml ) Need Help??
zemplen has asked for the wisdom of the Perl Monks concerning the following question:

I want to bind a change in the background property to an action. This:
$baseLabel->bind('<Property>=> sub { &setColors ()});
doesn't seem to work. I don't see a way to specify the Background property. The above code compiles but a change in the background color does not call setColors

Thanks

Comment on perl tk bind callback
Download Code
Re: perl tk bind callback
by ptkdb (Monk) on Oct 17, 2003 at 19:12 UTC
    The more complete the example, the more people are able to help. You need to find people dumb enough to write them for you.
    use strict ; use Tk ; sub setColors { print "set Colors @_!\n" ; } sub setConfiguration { print "setConfiguration @_\n" ; } my($mw) = new MainWindow ; my($l) = $mw->Button(-text => 'Hello World')->pack ; $l->bind('<Property>' => \&setColors) ; $l->bind('<Configure>' => \&setConfiguration) ; $l->configure(-command => sub { $l->configure(-background => 'blue') ; +}) ; MainLoop ;
    That said, the setConfigure sub seems to be called, but not the setProperty sub. However, in the PerlTk documenation, things like -background are referred to as OPTIONS and I'm wondering if the distinction is pertinant.
Re: perl tk bind callback
by converter (Priest) on Oct 17, 2003 at 19:26 UTC

    You can create your own class derived from Tk::Label and override the configure method.

    #!/usr/bin/perl use warnings; use strict; use Tk; { package Tk::MyLabel; use Tk::widgets('Label'); @Tk::MyLabel::ISA = 'Tk::Label'; Tk::Widget->Construct('MyLabel'); sub configure { my ($cw, %args) = @_; if (my $bg = $args{-background} || $args{-bg}) { print "changing background to: $bg\n"; } $cw->SUPER::configure(%args); } } my (@color, $color) = qw(blue green); my $mw = tkinit; $mw->packPropagate(0); my $lab = $mw->MyLabel( -text => "foo", -background => $color[0], )->pack; my $b = $mw->Button( -text => 'Do it!', -command => sub { $color = $color ? 0 : 1; $lab->configure(-background => $color[$color]); } )->pack; $mw->bind('<Control-q>', sub { Tk::exit(0) }); MainLoop;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-09-23 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls