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

Hi (again). I am using the Tk module with a Checkbutton routine as follows;

# Set up frames to contain all the pids (for selection) my $frame_pids; $frame_pids = $main->Frame(-borderwidth => 2, -relief => 'groove'); my @sortedKeys = ( sort keys %IdxToNickName ); for my $k (@sortedKeys) { my $pid_nickname = $IdxToNickName{$k}; my $cb = $frame_pids->Checkbutton( -text => sprintf("%-15s",$pid_nickname), # courier and -15 implies fixed length left justifi +ed -variable => \$IdxToNickNameSel{$k}, # this is a hash for the selection result (so other +s can access) -font => ['courier', 10, 'bold'] # and a font size ); $IdxToNickNameSel{$k} = 0; # Initialise the selection to 0 $cb->pack(-side => 'top'); # Pack the cb according to left c +riteria }

This permits me to have the checkbutton selections reference by the whole of the program (using "-variable"). For my next step I would like to selectively product the results and load into a hash which in turn is reflected onto the screen. What is the best way to achieve this output ??

I expect the "-variable" option needs to be available, this this does not seem to be available for Text and Labels.

Regards JC......

Replies are listed 'Best First'.
Re: Perl Tk module; optional select to result presentation
by GrandFather (Saint) on Jan 22, 2025 at 08:37 UTC

    Not quite sure what you are wanting to achieve, but it sounds like you want to populate label and text widgets from %IdxToNickNameSel and possibly update those widgets as checkbutton values change. If that is the case then probably the easiest way to do that is write a sub that configures the contents of the labels and test widgets from the hash. You then call that sub to populate the widgets to start with, then again whenever contents change.

    To get the update to happen on content change you need to add a -command handler to each checkbutton:

    my $cb = $frame_pids->Checkbutton( -text => sprintf("%-15s",$pid_nickname), -variable => \$IdxToNickNameSel{$k}, -font => ['courier', 10, 'bold'], -command => sub {Refresh($k)} );

    This is untested code! The idea is whenever the Checkbutton changes Refresh is called with the appropriate key as a parameter.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Perl Tk module; optional select to result presentation
by tybalt89 (Monsignor) on Jan 22, 2025 at 13:31 UTC

    Something like this?

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11163804 use warnings; use Tk; use Data::Dump qw( pp ); my %IdxToNickName = qw( a one b two c three ); my %IdxToNickNameSel; my $main = MainWindow->new; # Set up frames to contain all the pids (for selection) $main->Label(-text => 'Below are the CheckButtons', -bg => 'green', )->pack(-fill => 'x'); my $frame_pids; $frame_pids = $main->Frame(-borderwidth => 2, -relief => 'groove'); $frame_pids->pack; $main->Label(-text => 'Below is a Text widget', -bg => 'green', )->pack(-fill => 'x'); my $edit = $main->Text()->pack; $main->Label(-text => 'Below is a grid of Labels', -bg => 'green', )->pack(-fill => 'x'); my $labelframe = $main->Frame->pack; my @sortedKeys = ( sort keys %IdxToNickName ); my $row = 1; for my $k (@sortedKeys) { my $pid_nickname = $IdxToNickName{$k}; my $cb = $frame_pids->Checkbutton( -text => sprintf("%-15s",$pid_nickname), # courier and -15 implies fixed length left justifi +ed -variable => \$IdxToNickNameSel{$k}, # this is a hash for the selection result (so other +s can access) -font => ['courier', 10, 'bold'], # and a font size -command => \&showtext, ); $IdxToNickNameSel{$k} = 0; # Initialise the selection to 0 $cb->pack(-side => 'top'); # Pack the cb according to left c +riteria $labelframe->Label(-text => $IdxToNickName{$k}, )->grid(-row => $row, -column => 1, -sticky => 'e'); $labelframe->Label(-textvariable => \$IdxToNickNameSel{$k}, )->grid(-row => $row, -column => 2); $row++ } $main->Button(-text => 'Exit', -command => sub{$main->destroy}, -bg => 'red', -fg => 'white', )->pack(-fill => 'x'); showtext(); MainLoop; sub showtext { $edit->Contents( pp \%IdxToNickNameSel ) }
Re: Perl Tk module; optional select to result presentation
by Anonymous Monk on Jan 22, 2025 at 09:29 UTC