#!/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; }