# tk_centered_frame_3.pl 28may19waw use strict; use warnings; use Tk; use Tk::ProgressBar; my %Contents = ( # various fake content percent => [ 0, 1, 50, 90, 100, 120, ], message => [ # user messages 'msg 1', 'user msg 2', 'user message 3 (50%)', 'user message four (90%)', 'user message five (100 percent)', 'really long user message six (120 percent progress)', ], ); die "fake messages and percentages don't match" if @{ $Contents{message} } != @{ $Contents{percent} }; my $MW = MainWindow->new(); my $StatusBar = $MW->Frame() ->pack(qw(-side top -expand 1 -fill x)); use constant STATUS_SUBFRAME_PACK => qw(-side left -expand 1 -fill x); # define 3 sub-frames in status bar frame. use constant PAGING_SUBFIELD_WIDTH => 9; use constant STATUS_SUBFRAME_WIDTH => PAGING_SUBFIELD_WIDTH * 3; my $UserMessage = "UNDEFINED user message"; my $msg = $StatusBar->Label( -anchor => 'w', -relief => 'flat', -width => STATUS_SUBFRAME_WIDTH, -textvariable => \$UserMessage, )->pack(STATUS_SUBFRAME_PACK); my $paging = $StatusBar->Frame() # to be filled with sub-frames ->pack(STATUS_SUBFRAME_PACK); my $percent_done = undef; my $progress = $StatusBar->ProgressBar( -width => STATUS_SUBFRAME_WIDTH, -length => 200, # -length => STATUS_SUBFRAME_WIDTH, -anchor => 'w', -from => 0, -to => 100, -blocks => 1, -colors => [ 0 => '#93ff66', 50 => '#ffff66', 80 => '#dc143c', ], -variable => \$percent_done )->pack(STATUS_SUBFRAME_PACK); # define 3 sub-frames for paging buttons. use constant PAGING_BUTTONS_PACK => qw(-side left -expand 1 -fill both -anchor center); my $ButtonPagingPrevious = $paging->Button( -text => '< Previous', -width => PAGING_SUBFIELD_WIDTH, -command => \&dec_i_content, )->pack(PAGING_BUTTONS_PACK); my $PagingMessage = 'UNDEFINED paging'; my $WidgetShowPaging = $paging->Label( -relief => 'flat', -width => PAGING_SUBFIELD_WIDTH, -textvariable => \$PagingMessage, )->pack(PAGING_BUTTONS_PACK); my $ButtonPagingNext = $paging->Button( -text => 'Next >', -width => PAGING_SUBFIELD_WIDTH, -command => \&inc_i_content, )->pack(PAGING_BUTTONS_PACK); init_fake_content(); $MW->MainLoop(); exit; # subroutines ###################################################### { # begin closure for faking various content my $i_content = 0; sub init_fake_content { $i_content = @_ ? shift : 0; update_user_msg_and_progress($i_content); update_paging_msg($i_content); } sub inc_i_content { ++$i_content; $i_content = 0 if $i_content > $#{ $Contents{percent} }; update_user_msg_and_progress($i_content); update_paging_msg($i_content); } sub dec_i_content { --$i_content; $i_content = $#{ $Contents{percent} } if $i_content < 0; update_user_msg_and_progress($i_content); update_paging_msg($i_content); } sub update_user_msg_and_progress { my $i = shift; $percent_done = $Contents{percent}[$i]; $UserMessage = $Contents{message}[$i]; } sub update_paging_msg { my $i = shift; $PagingMessage = sprintf '%d of %d', 1+$i, scalar @{ $Contents{message} }; } } # end closure for faking various content