Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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


Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://300074]
Approved by HyperZonk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-03-25 02:19 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.