http://www.perlmonks.org?node_id=688942

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

Dear monks I still working on my pet project due to some other projects i put this aside but again I took a look a new perpective i dont care how many file I need to edit to change the status of the banks or operations, so I did these little code
use Tk; $mw->title("Bank status"); $mw->geometry("1024x764"); my $upperframe = $mw->frame()-pack(-fill => 'x'); Tk::MainLoop(); sub build_entry_bank{ my $statusbank = shift; my $box_banamex = $upperframe->Entry( -forecolor => 'blue', -textvariable => \$statusbank, -width => 20, )->pack (-side => 'left', -expand => 1); return $box_banamex; } sub status_bank{ open (DATBANK, "banamex.txt") || die ("File Bank did not open! +!!!"); my @bank_status = <DATBANK>; close (DATBANK); foreach my $status_bank(@bank_status){ chomp ($status_bank); my $statusbank; my $bank; ($bank,$statusbank) = split(/\|/,$status_bank); } }
now these code has some things missing, one of them how to make the data on the sub status_bank being use on the sub build_entry_bank and later how to make the data on sub status_bank can by refresh every one minute with repeat does anyone have any pointers or teach me it will be apreciatted

thanks

Replies are listed 'Best First'.
Re: Refresh data ina entry widget
by Popcorn Dave (Abbot) on May 29, 2008 at 02:09 UTC
    I believe what you want to do is $mw->update. That should write your changes to the screen once you make them in your data.


    Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

    I would love to change the world, but they won't give me the source code

Re: Refresh data ina entry widget
by zentara (Archbishop) on May 29, 2008 at 11:38 UTC
    Whenever you want a refresh on a time period in Tk, you probably want a timer.
    $mw->repeat(1000, \&status_bank); sub status_bank{ ......... do your extraction from file $mw->update; }
    If you have alot of banks and entries, put them all in a hash, and loop thru them, like this pseudocode:
    my %box_banamex; $box_banamex{$bank}{'entry'} = $upperframe->Entry(......... -textvariable => \$box_banamex{$bank}{'status'}, foreach my $bank (keys %box_banamex){ #read each value from file $box_banamex{$bank}{'status'} = whatever from file parsing; $box_banamex{$bank}{'entry'}->update; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Hi
      I am trying really trying but can you please elaborate more on the pseudocode because i dont get it, please?
        Pseudocode means "this is a non-functional shorthand view" of the steps you would need to take to write working code. I don't particularly like being asked to write code for someone who is getting paid for it. So here is a general purpose example, if you can't figure out the rest, you should read some books.
        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my %data; foreach my $bank qw(aa bb cc dd){ $data{$bank}{'frame'} = $mw->Frame->pack(); $data{$bank}{'bankname'} = $bank; $data{$bank}{'status'} = 'offline'; $data{$bank}{'label'} = $data{$bank}{'frame'}->Label( -textvariable => \$data{$bank}{'bankname'})->pack(-side=>'left') +; $data{$bank}{'entry'} = $data{$bank}{'frame'}->Entry( -textvariable => \$data{$bank}{'status'}, -bg => 'white', )->pack(-side=>'right'); } $mw->repeat(2000, sub{ foreach my $bank (keys %data){ my $rand = rand 10; if ($rand > 5){ $data{$bank}{'status'} = 'online'; $data{$bank}{'entry'}->configure(-fg => 'red'); }else{ $data{$bank}{'status'} = 'offline'; $data{$bank}{'entry'}->configure(-fg => 'black'); } } }); MainLoop;

        I'm not really a human, but I play one on earth CandyGram for Mongo
Re: Refresh data ina entry widget
by starbolin (Hermit) on May 29, 2008 at 03:43 UTC

    The subroutine status_bank does nothing to store or preserve its data. It's not normal for the subroutine that reads the data from a file to also handle displaying the data. In general, you don't know when your display object is going to get called to redraw it's frame so you need to store your data in some kind of data stucture. Then a callback routine called by an event handler fetches data from the data structure and writes it to your display object string. I see none of this structure in your code so I am at a bit of a loss as to what to recommend.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Refresh data ina entry widget
by starbolin (Hermit) on May 29, 2008 at 03:49 UTC

    padawan linuxero writes:

    "... how to make the data on sub status_bank can by refresh every one minute with repeat ..."

    Once you have a signal handler in place to refresh your diplay from your database, it is trivial to send a signal periodically to refresh the frame.

    s/signal/event/


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}