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

Monks,

I am working on an application that has 2 text boxes.

one gathers data: $config_text
the other shows the results: $results_text

When a button is pressed the data from that test box is retrieved and stored in an array. After some checks all the data is submitted to the proper sub routine and in each one Net::Telnet::Cisco is called and executes it's foreach loop roughly 20-100 times. Inside the foreach loop I call $results_text->insert('end', "update");. The problem is I won't see the text until the foreach loop has completed all of its runs.

I tried using the after() listed in the following post, but had no luck.

http://www.perlmonks.org/index.pl?node_id=65969

However,I could have implemented it wrong?

Thanks,
Any help or suggestions is appreciated, full source:

#!/usr/bin/perl #################################### #Date: 5/16/2003 # #Author: Jason Riedel # #Title: Cisco Configuration Changer# ###################################################################### +# #Purpose: To globally change cisco configuration in a simple easy GUI. +# ###################################################################### +# use strict; use DBI; use Net::Telnet::Cisco; use Tk; require Tk::MesgBox; require Tk::Radiobutton; ####################### # VARIABLES # ####################### use vars qw /@config $radio $ip $routeruser $routerpass/; ########################### # Windows & Widgets # ########################### ########################### my $w = MainWindow->new(); $w->configure(-title => 'Cisco Configuration Changer v1.0', -backgroun +d => 'gray'); $w->geometry('+250+250'); ## Create Window Frame ## my $menu_bar_frm = $w->Frame(-relief=> 'groove', -borderwidth=>3, -bac +kground=>'gray')->pack(-side => 'top', -fill=>'x'); ## Create File Menu ## my $file_button = $menu_bar_frm->Menubutton(-text=> 'File', -underline +=> 0, -background=> 'gray', -activebackground=> 'white', -foreground= +> 'blue')->pack(-side => 'left'); my $file_menu = $file_button->Menu(); ## Create Option Exit ## $file_menu->command(-command => \&end, -label => "Exit", -underline => + 0); $file_button->configure(-menu=>$file_menu); ## Create Help Menu ## my $help_button = $menu_bar_frm->Menubutton(-text=>"Help", -underline +=> 0, -background=> 'gray', -activebackground=> 'white', -foreground= +>'blue')->pack(-side => 'left'); my $help_menu = $help_button->Menu(); ## Create Option About ## $help_menu->command(-command => \&about, -label => "About", -underline + => 0); $help_button->configure(-menu=>$help_menu); ## Create Frames ## my $w_radio_frm = $w->Frame(-relief=> 'groove', -borderwidth=>0, -back +ground=>'gray')->pack(-side => 'top', -fill=>'x'); my $w_login_frm = $w->Frame(-relief=> 'groove', -borderwidth=>0, -back +ground=>'gray')->pack(-side => 'top', -fill=>'x'); my $w_config_frm = $w->Frame(-relief=> 'groove', -borderwidth=>0, -bac +kground=>'gray')->pack(-side => 'left', -fill=>'x'); my $w_results_frm = $w->Frame(-relief=> 'groove', -borderwidth=>0, -ba +ckground=>'gray')->pack(-side => 'left', -fill=>'x'); ## Create Single Label & Entry ## my $single_label = $w_radio_frm->Label(-text => 'Single IP:', -backgro +und => 'gray', -foreground => 'blue')->pack(-side => 'left'); my $single_entry = $w_radio_frm->Entry(-textvariable=> \ $ip)->pack(-s +ide => 'left'); ## Create Radio Buttons ## my $cmts_button = $w_radio_frm->Radiobutton(-text=> 'CMTS\'s', -value= +> 'CMTS\'s', -variable=> \$radio)->pack(-side => 'left'); my $cores_button = $w_radio_frm->Radiobutton(-text=> 'Cores', -value= +> 'Cores', -variable=> \$radio)->pack(-side => 'left'); my $switches_button = $w_radio_frm->Radiobutton(-text=> 'Switches', - +value=> 'Switches', -variable=> \$radio)->pack(-side => 'left'); my $all_button = $w_radio_frm->Radiobutton(-text=> 'All', -value=> 'A +ll', -variable=> \$radio)->pack(-side => 'left'); ## Create Login & Password Label & Entry ## my $user_label = $w_login_frm->Label(-text => 'Username:', -backgroun +d => 'gray', -foreground => 'blue')->pack(-side => 'left'); my $user_entry = $w_login_frm->Entry(-textvariable => \ $routeruser, - +width => '20')->pack(-side => 'left'); my $pass_label = $w_login_frm->Label(-text => 'Password:', -backgroun +d => 'gray', -foreground => 'blue')->pack(-side => 'left'); my $password_entry = $w_login_frm->Entry(-textvariable => \ $routerpas +s, -width => '20', -show => '*')->pack(-side => 'left'); ## Create Entry Box ## my $config_entry = $w_config_frm->Scrolled('Text', -width => '50', -he +ight => '40')->pack(-side => 'left'); ## Create Text Box ## my $results_text = $w_results_frm->Scrolled('Text', -width => '40', -h +eight => '40')->pack(-side => 'right'); ## Create Buttons ## my $config_button = $w_config_frm->Button(-text=>'Config', -background +=>'gray', -command => \ &config)->pack(-side => 'left', -anchor=> 'ce +nter'); ########## # Config # ########## ########## sub config { @config = (); @config = $config_entry->get('1.0', 'end'); #################### # SQL DATA # #################### my $dsn = "DBI:mysqlPP:db:host"; my $dbuser = "dbuser"; my $dbpass = "dbpass"; my $dbh = DBI->connect($dsn,$dbuser,$dbpass) or die ("could not co +nnect to sql"); my $cmts = $dbh->prepare( qq |SELECT system, ip FROM systems WHERE + device="CMTS"| ) or die ("could not connect to sql"); my $core = $dbh->prepare( qq |SELECT system, ip FROM systems WHERE + device="Core"| ) or die ("could not connect to sql"); my $switch = $dbh->prepare( qq |SELECT system, ip FROM systems WHE +RE device="Switch"| ) or die ("could not connect to sql"); if (defined($routeruser) and defined($routerpass)) { if (defined($ip)) { $results_text->delete('1.0', 'end'); $results_text->insert('end', "Running the submitted config +uration for: $ip\n\n"); single(); } elsif ($radio eq "CMTS\'s") { $results_text->delete('1.0', 'end'); $results_text->insert('end', "Running the submitted config +uration for: All CMTS\'s\n\n"); cmtss($cmts); } elsif ($radio eq "Cores") { $results_text->delete('1.0', 'end'); $results_text->insert('end', "Running the submitted config +uration for: All Cores\n\n"); cores($core); } elsif ($radio eq "Switches") { $results_text->delete('1.0', 'end'); $results_text->insert('end', "Running the submitted config +uration for: All Switches\n\n"); switches($switch); } elsif ($radio eq "All") { $results_text->delete('1.0', 'end'); $results_text->insert('end', "Running the submitted config +uration for: All\n\n"); all($cmts, $core, $switch); } } else { $results_text->delete('1.0', 'end'); $results_text->insert('end', "Username and Password were not d +efined and must be to run.\n"); } $ip = undef; } MainLoop; ########## # Single # ########## ########## sub single { eval { my $cisco = Net::Telnet::Cisco->new(Host => $ip); $cisco->login("$routeruser", "$routerpass"); $cisco->cmd( 'terminal length 0' ); $cisco->cmd( 'config t' ); $results_text->insert('end', "$ip - done\n"); foreach my $command (@config) { @config = $cisco->cmd($command); } $cisco->close; }; if ($@) { $results_text->insert('end', "\n$ip Errored: $@\n\n"); } } ########## # CMTS's # ########## ########## sub cmtss { my (%cmts, $system); my $cmts = shift; $cmts->execute(); while (my $row = $cmts->fetchrow_arrayref() ) { $cmts{$row->[0]} = $row->[1]; } my @cmts = sort keys %cmts; eval { foreach $system (@cmts) { my $cisco = Net::Telnet::Cisco->new(Host => $cmts{$system +}); $cisco->login("$routeruser", "$routerpass"); $cisco->cmd( 'terminal length 0' ); $cisco->cmd( 'config t' ); foreach my $command (@config) { @config = $cisco->cmd($command); } $cisco->close; $results_text->insert('end', "$system - done\n"); } }; if ($@) { $results_text->insert('end', "\n$system Errored: $@\n\n"); } } ######### # Cores # ######### ######### sub cores { my (%core, $system); my $core = shift; $core->execute(); while (my $row = $core->fetchrow_arrayref() ) { $core{$row->[0]} = $row->[1]; } my @core = sort keys %core; eval { foreach $system (@core) { my $cisco = Net::Telnet::Cisco->new(Host => $core{$system +}); $cisco->login("$routeruser", "$routerpass"); $cisco->cmd( 'terminal length 0' ); $cisco->cmd( 'config t' ); $results_text->insert('end', "$system - done\n"); foreach my $command (@config) { @config = $cisco->cmd($command); } $cisco->close; } }; if ($@) { $results_text->insert('end', "\n$system Errored: $@\n\n"); } } ############ # Switches # ############ ############ sub switches { my (%switch, $system); my $switch = shift; $switch->execute(); while (my $row = $switch->fetchrow_arrayref() ) { $switch{$row->[0]} = $row->[1]; } my @switch = sort keys %switch; eval { foreach $system (@switch) { my $cisco = Net::Telnet::Cisco->new(Host => $switch{$syst +em}); $cisco->login("$routeruser", "$routerpass"); $cisco->cmd( 'terminal length 0' ); $cisco->cmd( 'config t' ); $results_text->insert('end', "$system - done\n"); foreach my $command (@config) { @config = $cisco->cmd($command); } $cisco->close; } }; if ($@) { $results_text->insert('end', "\n$system Errored: $@\n\n"); } } ######### # All # ######### ######### sub all { my (%cmts, %cores, %switches, $system); my $cmts = shift; my $core = shift; my $switch = shift; $cmts->execute(); $core->execute(); $switch->execute(); while (my $row = $cmts->fetchrow_arrayref() ) { $cmts{$row->[0]} = $row->[1]; } while (my $row = $core->fetchrow_arrayref() ) { $cores{$row->[0]} = $row->[1]; } while (my $row = $switch->fetchrow_arrayref() ) { $switches{$row->[0]} = $row->[1]; } my @cmts = sort keys %cmts; my @core = sort keys %cores; my @switch = sort keys %switches; eval { foreach $system (@cmts) { my $cisco = Net::Telnet::Cisco->new(Host => $cmts{$system +}); $cisco->login("$routeruser", "$routerpass"); $cisco->cmd( 'terminal length 0' ); $cisco->cmd( 'config t' ); $results_text->insert('end', "$system - done\n"); foreach my $command (@config) { @config = $cisco->cmd($command); } $cisco->close; } $results_text->insert('end', "\nRunning the submitted configur +ation for: All Cores\n\n"); foreach $system (@core) { my $cisco = Net::Telnet::Cisco->new(Host => $cores{$syste +m}); $cisco->login("$routeruser", "$routerpass"); $cisco->cmd( 'terminal length 0' ); $cisco->cmd( 'config t' ); $results_text->insert('end', "$system - done\n"); foreach my $command (@config) { @config = $cisco->cmd($command); } $cisco->close; } $results_text->insert('end', "\nRunning the submitted configuratio +n for: All Switches\n\n"); foreach $system (@switch) { my $cisco = Net::Telnet::Cisco->new(Host => $switches{$sy +stem}); $cisco->login("$routeruser", "$routerpass"); $cisco->cmd( 'terminal length 0' ); $cisco->cmd( 'config t' ); $results_text->insert('end', "$system - done\n"); foreach my $command (@config) { @config = $cisco->cmd($command); } $cisco->close; } }; if ($@) { $results_text->insert('end', "\n$system Errored: $@\n\n"); } } ######### # About # ######### ######### sub about { my $mesg = $w->MesgBox( -title => 'About', -text => "Author: Jason Riedel\nVersion: 1.0\nReleased 5/16 +/2003\nPurpose: To change cisco configuration for our cisco devices i +n a simple easy GUI.\n", -buttons => ['OK'] ); $mesg->Show; } ######### # End # ######### ######### sub end { exit; }

Replies are listed 'Best First'.
Re: Tk Widget Updating from a foreach loop
by BrowserUk (Patriarch) on May 17, 2003 at 08:54 UTC

    Try putting a $w->update; after each $results_text->insert(...).

    You would probably have gat a reply more quickly if you had reduced your problem to a minimal demonstration rather than expecting people to wade through the whole application.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      That worked! I apologize for the full source, I just didn't want to give to little. Thanks for the advice, I was hoping it was something simple, I really appreciate it.