Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Perl Tk , MainLoop, destroy function problem

by zentara (Archbishop)
on Feb 24, 2013 at 13:03 UTC ( #1020395=note: print w/ replies, xml ) Need Help??


in reply to Perl Tk , MainLoop, destroy function problem

Hi again, I realize you are a beginner at this gui business, so in lieu of trying to solve your logic problem with the code as you have written it, I would like to suggest to you an alternative program flow structure, which would be cleaner.

You don't need 5 subs to do the refresh, where you have clear_data(), _refreshdata(), read_file(), open_report(), and finally _insertData().

You can more easily do it in one sub called refresh(), which is called by the timer. Now in my code flow, refresh() will read the file, then just insert the data in its appropriate $path. You will need a few tests in there, requiring that if the $path already exists, you will reconfigure that path; and if the path dosn't exist, then an addpath is used. It should all be done in 1 sub for maintaining easy flow.

Also 1 final point, you don't distinquish between users in the rows. You data seems to have multiple users, and I would have the user as the data in the first column, where you put it in a label above the Hlist. But it's your code, and you know what needs to be done.

#!/usr/bin/perl use Tk; use Tk::HList; use Tk::ItemStyle; use Data::Dumper; my $user = $ARGV[0] || 'alex'; my $hash = {}; my ($location,$age,$use,$vendor,$feature); my $sec = 5000; #default value #gui variables my ($hl,$ok,$alert); # Making the Gui my $mw = new MainWindow; $mw->geometry("500x200"); my $userframe = $mw->Frame(-width=>5,-height=>10)->pack(-side=>'top',- anchor=>'nw'); $userframe->Label(-text => "USER: $user")->pack(-side => 'left', -anch +or => 'nw',-padx => 0); $userframe->Label(-text => "Set time")->pack(-side => 'left',-anchor = +> 'w',-padx => 0); my $frequency = $userframe->Entry(-width=>5,-textvariable=> \$sec) ->pack(-side => 'left',-anchor => 'nw',-padx => 0); my $hlistframe = $mw->Frame()->pack(-fill => 'both', -expand => 1); $hl = $hlistframe->Scrolled('HList', -scrollbars => 'ose', -columns =>4 , -header => 1, -width => 100, -command => sub {print "AAA\n";}, -selectmode => 'browse', )->pack(-fill => 'both',-expand =>1 ); my $label1 = $hl->Label(-text => "location", -anchor => 'w'); $hl->headerCreate(0,-itemtype => 'window',-widget => $label1); my $label3 = $hl->Label(-text => "Age", -anchor => 'w'); $hl->headerCreate(1,-itemtype => 'window',-widget => $label3); my $label4 = $hl->Label(-text => "phone", -anchor => 'w'); $hl->headerCreate(2,-itemtype => 'window',-widget => $label4); refresh(); # called to start the first refresh # the timer is now at the end of the refresh() sub MainLoop; sub refresh { # 1 here you read the file # 2 do your regexes # 3 test to see how many paths you have thru info('children') # 4 reconfigure the existing paths with new data # 5 if new paths are needed add them, if you have too many # existing paths, hide them # finally call it again later # make a lower bound for timer # in case your user sets $sec to 0 if( $sec < 1000){ $sec = 1000 } $mw->after( $sec, \&refresh); } + +

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh


Comment on Re: Perl Tk , MainLoop, destroy function problem
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1020395]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-09-21 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls