Search code examples
perltk-toolkit

Perl subroutine is not updating/refreshing text interface with toggle button


I am very new to perl, and I am creating a grid button interface in which it involves fetching data of current SSID network connected. I am not getting any error, but it is not refreshing the current information.

Here is what I have tried, but it does not update mw.


use strict;
use warnings;
use Tk;
use Tk::LabFrame;
use Tk::widgets qw(LabFrame);
use POSIX;

my $mw= tkinit;

my $gridOrPack="grid";
my $tpFrame=$mw->LabFrame(-label=>'Info');
my $code_font = $mw->fontCreate(-family => 'arial', -size => 10);

if ($gridOrPack=~/grid/i) {
  $tpFrame->grid(-row=>1, -column=>1, -sticky=>'nsew');
  $mw->gridRowconfigure( 1, -weight => 1 );
  $mw->gridColumnconfigure( 1, -weight => 1 );
}
else {
  $tpFrame->pack(-side=>'top',-expand=>1,-fill=>'both');
}

my @buttons;
my $text1 = qx(Netsh WLAN show interface | grep -w  SSID | perl -pne s/.*?:.//);
chomp($text1);

push @buttons, $tpFrame->Label(-text=>  "Network  :   " .$text1." ",-font => ['arial', '10'],-justify => 'left')->pack(-side=>'left')
->grid
($tpFrame->Button(-text => 'Refresh Info', -bg => 'yellow', -width => 11, -height => 3, -command => sub { toggle(\$text1) }, -font => $code_font ));


MainLoop;

sub toggle {
    my $text_ref = shift;
    $$text_ref = qx(Netsh WLAN show interface | grep -w  SSID | perl -pne s/.*?:.//);
    chomp($$text_ref);
    return;
}

Solution

  • You can use the -textvariable option of Tk::Label. This connects the widgets text property with a perl variable to automatically reflect its content.

    push @buttons, $tpFrame->Label(-textvariable=>  \$text1,
                                   -font => ['arial', '10'],
                                   -justify => 'left')->pack(-side=>'left')
    

    etc.