Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Perl/Tk button color refresh using repeat...

by dbuckhal (Chaplain)
on Jun 04, 2012 at 06:01 UTC ( [id://974240]=perlquestion: print w/replies, xml ) Need Help??

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

Evenin' folks, I hope you all can help me out.

Here is my code to replicate my issue. You will see two sets of button grids. As you click the Switch button at the top, it toggles which grid is enabled or disabled. But, after re-enabling a grid, the button color remains "white", yet a mouse-over will restore the correct color. I found a code clip using the 'repeat' method to call a function to reconfigure the buttons back to their appropriate color, but it does not work. It does work when I use...

( -text => 'blue' )

...all of the buttons show "blue" labels, but no color change when I use..

( -foreground => 'blue' )

... so the repeat method does work, but not how I would like it to for button foreground color.

I did find posts with examples of 'repeat' and 'after', but nothing that suited my situation. Plus, since the repeat method does work on the text label test, there must be something else I am missing. It's mostly been just myself and the Mastering Perl/Tk book for a couple of months, now. :)

Thanks!

Derrick

#!/usr/bin/perl use strict; use warnings; use feature qw( state ); use Tk; my ( @bHits, @rHits ); my ( @bList, @rList ); state $turn = 0; my $mw = MainWindow->new( -title => 'Test Grid!' ); $mw->geometry( '1000x400+200+50' ); $mw->resizable( 0, 0 ); $mw->Label( -text => 'Test Grid', -foreground => 'black', )->pack( -side => 'top', -expand => '0', ); my $mw_switch_b = $mw->Button( -text => 'Switch', -command => sub { $turn ? disableBtns( \@bList, \@rList ) : disableBtns( \@rList, \@bList ); + } )->pack; my $mw_exit_b = $mw->Button( -text => 'Exit', -command => sub { exit } )->pack; my ( $blueFrame, $redFrame, $blHitFrame, $rdHitFrame ); $blueFrame = $mw->Frame; $redFrame = $mw->Frame; ### Blue Grid for my $row ( 0 .. 9 ) { for my $col ( 0 .. 9 ) {; $blueFrame->Button( -text => $col . $row, -background => 'light blue', -command => [ \&showBtn ], )->grid( -row => $row, -column => $col ); } } ### Red Grid for my $row ( 0 .. 9 ) { for my $col ( 0 .. 9 ) { $redFrame->Button( -text => $col . $row, -background => 'indian red', -command => [ \&showBtn ], )->grid( -row => $row, -column => $col ); } } $blueFrame->pack( -side => 'left', -padx => '10' ); $redFrame->pack( -side => 'right', -padx => '10' ); @bList = $blueFrame->gridSlaves(); @rList = $redFrame->gridSlaves(); $blueFrame->repeat( 1000, \&update_button_colors); $redFrame->repeat( 1000, \&update_button_colors); MainLoop; sub update_button_colors { foreach my $b ( @bList ) { $b->configure( -background => 'light blue' ); } foreach my $r ( @rList ) { $r->configure( -background => 'indian red' ); } return; } sub showBtn { ( $turn ) ? disableBtns( \@rList, \@bList ) : disableBtns( \@bList, \@rList ); return; } sub disableBtns { my @list = ( @_ ); # gridSlave references bList and rList for ( 0 .. 1 ) { foreach my $btn ( @{$list[$_]} ) { $_ ? $btn->configure( -state => 'disable' ) : $btn->configure( -state => 'active' ); } } $turn = !( $turn ); return; }

Replies are listed 'Best First'.
Re: Perl/Tk button color refresh using repeat...
by Anonymous Monk on Jun 04, 2012 at 06:48 UTC

    Its simple, you set the state to active when you should have set it to normal

    active means mouse is over it, disable means its disabled (greyed out), normal means enabled but no mouse is over it

    The opposite of disabled is normal

      That is what it is. I actually woke up this morning thinking about it some more, wondering about that word, 'active'. Also, I was stuck on the thought of how the buttons were not just their default color, but the same color as a mouseover event. I was "lighting up" the buttons, rather than returning them to 'normal'.

      Thanks! I'll try to post something more challenging next time! :)

Re: Perl/Tk button color refresh using repeat...
by choroba (Cardinal) on Jun 04, 2012 at 07:13 UTC

      Yes, something like that. Thanks for "another way to do it"! I appreciate your time and code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-24 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found