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

What I want to do is have a DialogBox widget appear when a new file is available. The problem i'm encountering is that the DialogBox widget is blinking, meaning you have to click yes or no multiple times before it disappears. I cant seem to fix the problem. Any ideas? he main parts involved ar the "DialogBox Display" and "Thread 2".
#!/usr/bin/perl ################################################# # This program is a Tk script that connects to a server and uploads th +e file # uavposition. It is ment to run on the Virtual Cockpit server. You mu +st have # the correct modules installed in order for the program to run correc +tly. # ################################################# use strict; use warnings; use Tk; use Net::SFTP::Foreign; use threads; use threads::shared; use Tk::Dialog; use Tk::LabFrame; use Tk::DialogBox; my $user:shared = 'uas'; my $server:shared = 'updraft.unl.edu'; my $uavlcl:shared = '/home/deadpickle/Desktop/Virtual Cockpit'; my $uavrmt:shared = '/home/uas/public_html'; my $lcl:shared = '/home/deadpickle/Desktop/Virtual Cockpit'; my $rmt:shared = '/home/uas/public_html'; my $go:shared = 0; my $die:shared = 0; my $progress:shared = 0; my $count:shared; my $ver:shared = 0; my $go2:shared = 0; my $curtime:shared = 0; my $wayput:shared = 0; my $thr1 = threads->new(\&sftp); my $thr2 = threads->new(\&wvf); #now setup Tk #main Window #main Thread my $mw = MainWindow->new(); $mw->protocol('WM_DELETE_WINDOW' => sub { &clean_exit }); $mw->configure(-title => 'VIRTUAL COCKPIT:SFTP'); $mw->geometry('+400+300'); #Menubar my $mbar = $mw -> Menu(); $mw -> configure(-menu => $mbar); my $file = $mbar -> cascade(-label=>"File", -underline=>0, -tearoff => + 0); my $ctrl = $mbar -> cascade(-label=>"Controls", -underline=>0, -tearof +f => 0); $file -> command(-label =>"Save Config", -underline => 0, -command +=> [\&save, "Save"], -accelerator => 'Ctrl-S'); $file -> command(-label =>"Open Config", -underline => 0, -command => +[\&load, "Load"], -accelerator => 'Ctrl-L'); $file -> command(-label =>"Exit", -underline => 0, -command => [\&clea +n_exit, "Quit"], -accelerator => 'Ctrl-Q'); $ctrl -> command(-label =>"Connect", -underline => 0, -command => [\&g +o, "Connect"], -accelerator => 'Ctrl-C'); $ctrl -> command(-label =>"Stop", -underline => 0, -command => [\&go, +"Stop"], -accelerator => 'Ctrl-T'); $mbar -> command(-label =>"About", -underline => 0, -command => [\&abo +ut, "About"]); #User entry my $login = $mw->LabFrame(-label => "Login", -labelside => 'acrosstop' +, -width => 110, -height => 50, -fg => 'blue')->pack(-side => 'top', +-fill => 'x', -pady => 3); my $usrlbl = $login->Label(-text => 'Username')->pack(-side => 'left') +; my $usreny = $login->Entry(-width=> 10, -textvariable => \$user)->pack +(-side => 'left', -pady => 3); #Server entry my $svrlbl = $login->Label(-text => 'Server')->pack(-side => 'left'); my $svreny = $login->Entry(-width=> 15, -textvariable => \$server)->pa +ck(-side => 'left', -pady => 3); #UAV my $loc = $mw->LabFrame(-label => "UAV", -labelside => 'acrosstop', -w +idth => 110, -height => 50, -fg => 'blue')->pack(-side => 'top', -fil +l => 'x', -pady => 3); my $uavlcllbl = $loc->Label(-text => 'Local')->pack(-side => 'left'); my $uavlcleny = $loc->Entry(-width=> 25, -textvariable => \$uavlcl)->p +ack(-side => 'left', -pady => 3); my $uavrmtlbl = $loc->Label(-text => 'Remote')->pack(-side => 'left'); my $uavrmteny = $loc->Entry(-width=> 25, -textvariable => \$uavrmt)->p +ack(-side => 'left', -pady => 3); #Waypoints my $loc2 = $mw->LabFrame(-label => "Waypoints", -labelside => 'acrosst +op', -width => 110, -height => 50, -fg => 'blue')->pack(-side => 'top +', -fill => 'x', -pady => 3); my $waylcllbl = $loc2->Label(-text => 'Local')->pack(-side => 'left'); my $waylcleny = $loc2->Entry(-width=> 25, -textvariable => \$lcl)->pac +k(-side => 'left', -pady => 3); my $wayrmtlbl = $loc2->Label(-text => 'Remote')->pack(-side => 'left') +; my $wayrmteny = $loc2->Entry(-width=> 25, -textvariable => \$rmt)->pac +k(-side => 'left', -pady => 3); #Connect button my $btn = $mw->Button(-text => 'Connect', -command => \&go)->pack(-sid +e => 'bottom', -padx => 3, -pady => 3, -anchor => 'e'); #Scrolled Listbox my $val = 0; my $box = $mw->Scrolled("Listbox", -scrollbars => "oe",-width => 40, - +height => 7, -relief => 'groove', -bg => 'white')->pack(-side => "lef +t"); #timer to display messages my $timer1 = $mw->repeat(1000,\&msg); my $timer2 = $mw->repeat(1000,\&verway); my $timer3 = $mw->repeat(1000,\&time); #shortcuts $mw->bind('<Control-Key-s>', sub{&save}); $mw->bind('<Control-Key-l>', sub{&load}); $mw->bind('<Control-Key-c>', sub{&go}); $mw->bind('<Control-Key-t>', sub{&go}); $mw->bind('<Control-Key-q>', sub{&clean_exit}); MainLoop; #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ #Grabs current time sub time { my @time = gmtime(); my $mon = $time[4]; my $day = $time[3]; my $hour = $time[2]; my $min = $time[1]; my $sec = $time[0]; $mon = $mon + 1; if ($sec < 10){ $sec = "0$sec"; } if ($min < 10){ $min = "0$min"; } if ($hour < 10){ $hour = "0$hour"; } if ($day < 10){ $day = "0$day"; } $curtime = "$mon$day$hour$min$sec"; } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ sub about{ my $whatisthis = $mw->Dialog(-text => "Virtual Cockpit Control Pa +nel\nVersion 1.0\nAuthor: Jamie Lahowetz\n\nControl Panel used on Vir +tual Cockpit machine", -default_button => 'Close', -buttons => ['Clos +e']); my $choice = $whatisthis->Show; } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ sub save{ my @types = (["Config Files", '.vc', 'VC'], ["All Files", "*"] ); my $file = $mw->getSaveFile(-filetypes => \@types); if(defined $file){ open(OUT,">$file"); print OUT "$user\n"; print OUT "$server\n"; print OUT "$uavlcl\n"; print OUT "$uavrmt\n"; print OUT "$lcl\n"; print OUT "$rmt\n"; close OUT; } } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ sub load{ my $file; my @types = (["Config Files", '*.vc', 'VC'], ["All Files", "*"] ); $file = $mw->getOpenFile(-filetypes => \@types); if(defined $file){ open IN,$file; my @tfile = <IN>; chop @tfile; $user = $tfile[0]; $server = $tfile[1]; $uavlcl = $tfile[2]; $uavrmt = $tfile[3]; $lcl = $tfile[4]; $rmt = $tfile[5]; close IN; } } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ sub msg{ $val = $progress; if($val == 1){ $box->insert('end', "Connected: $user @ $server"); $box->see('end'); $progress = 0; } if($val == 2){ $box->insert('end', "Uploading: uavposition"); $box->see('end'); $progress = 0; } if($val == 3){ $box->insert('end', "Downloading: waytemp"); $box->see('end'); $progress = 0; } if($val == 4){ $box->insert('end', "Uploading New wayfinal"); $box->see('end'); $progress = 0; } if($val == 5){ $box->insert('end', "Connecting... Asking for Password..."); $box->see('end'); $progress = 0; } if($val == 6){ $box->insert('end', "Transfer Stopped"); $box->see('end'); $progress = 0; } if($val == 7){ $box->insert('end', "Could Not Connect to Server!!"); $box->see('end'); $progress = 0; $go = 0; go(); } if($val == 8){ $box->insert('end', "Disconnected from Server!!"); $box->see('end'); $progress = 0; $go = 0; go(); } } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ #DialogBox display sub verway { if ( $ver == 1){ my $dbox = $mw->DialogBox( -title=>'Verification', -buttons=>[ +"Yes", "No"]); my $ans = $dbox->Show; if ( $ans eq "Yes") { $ver = 2; } else { $ver = 0 } } } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ sub go { if($btn->cget(-text) eq 'Connect'){ $go = 1; $btn->configure(-text=> 'Stop'); $progress = 5; } else{ $go = 0; $go2 = 0; $btn->configure(-text=> 'Connect'); } } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ #Thead 1 sub sftp{ $|++; while(1){ if ( $die == 1 ){ goto END }; if ( $go == 1 ){ my $seconds = 120; my %args = (host=> $server, user=>$user, timeout=>$seconds +); my $sftp = Net::SFTP::Foreign->new(%args); if ($sftp->error){ $progress =7; if( $progress == 7){ for(;;){if($progress == 0){last}} goto line; } } $go2 = 1; #starts the waypoint verification $progress = 1; if( $progress == 1){ for(;;){if($progress == 0){last}} } for(;;){ $sftp->put("$uavlcl/uavposition", "$uavrmt/uavposition +"); $progress = 2; $count++; if( $progress == 2){ for(;;){if($progress == 0){last}} } if ( $wayput == 1) { $sftp->put("$lcl/wayfinal", "$rmt/wayfinal"); $progress = 4; if( $progress == 4){ for(;;){if($progress == 0){last}} } $wayput = 0; } if($count == 5){ #$sftp->get("$rmt/waytemp", "$lcl/waytemp"); #$progress = 3; #if( $progress == 3){ # for(;;){if($progress == 0){last}} #} $count = 0; } sleep 1; if ($sftp->error){ $progress =8; if( $progress == 8){ for(;;){if($progress == 0){last}} goto line; } } line: if($go == 0){last} if($die == 1){ goto END }; } undef $sftp; $progress = 6; if( $progress == 6){ for(;;){if($progress == 0){last}} } $go = 0; $progress = 0; }else { sleep 1 } } END: } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ #Thread 2 sub wvf { # $|++; while(1){ if($die == 1){ goto END }; if($go2 == 1){ for(;;){ #Open the waytemp file #Grab the time stamp open TEMP, '<', 'waytemp' or die "Cannot Open File!"; while ( my @temp = <TEMP>){ chomp @temp; my $time = $temp[0]; my $elements = @temp; #Compare times #new waytemp if ($time >= $curtime){ $ver = 1; if ( $ver == 1) { for (;;){ if ($ver == 2) {last} if ($ver == 0) { goto NO} } } #Yes open FINAL, '>', 'wayfinal' or die "Cannot Ope +n File!"; print FINAL "$curtime\n"; #load lat/long for ( my $cycle = 1; $cycle <= ($elements - 1) +; $cycle++){ print FINAL "$temp[$cycle]\n"; } $ver = 0; close FINAL; $wayput = 1; } else { sleep 1 } } NO: close TEMP; if ( $go2 == 0) { last } if ( $die == 1 ) {goto END } } $go2 = 0; $ver = 0; $wayput = 0; } else { sleep 1 } } END: } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ #clean Program exit sub clean_exit{ $timer1->cancel; $timer2->cancel; $timer3->cancel; my @running_threads = threads->list; if (scalar(@running_threads) < 1){exit } else{ $die = 1; $thr1->join; $thr2->join; exit; } }

Replies are listed 'Best First'.
Re: Verification DialogBox
by zentara (Cardinal) on May 23, 2007 at 19:53 UTC
    The problem i'm encountering is that the DialogBox widget is blinking, meaning you have to click yes or no multiple times before it disappears. I cant seem to fix the problem. Any ideas?

    From the description of your problem (and a quick look at the code which I can't run), the "blinking" is actually multiple DialogBoxes popping up in succesion until the $ver is set right. You have

    if ( $ver == 1) { for (;;){ if ($ver == 2) {last} if ($ver == 0) { goto NO} }
    in a thread, then in main you are popping the DialogBox depending on the value of $ver from a timer (every second).

    Fix your verway timer callback. In your sub verway(called by a timer), have a check so that if the DialogBox is currently displayed, it won't display it again. It's as simple as setting a global $is_dialog_up to 0 or 1.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      I agree with what zentara said -- I think its the timer. You could also set $ver to something other than 1 as soon as you enter the if; that way if the timer does go off before the user presses yes or no then "if ($ver==1)" won't be true.