OK, I've made changes and trimmed back to bare essentials. I am looping over the %tools hash to create tabs with parameters and defaults. So far, so good. Now, I would like to capture any user changes/inputs and write a text file consisting of:
LABEL=ENTRY\n
LABEL=ENTRY\n ...
#!/usr/bin/perl -w
use warnings;
use strict;
use Tk;
use Tk::NoteBook;
require Tk::Pane;
#use Tie::IxHash;
use Tk::LabEntry;
my $mw = MainWindow->new;
$mw->geometry( "600x500");
$mw->title("Survival Kit");
###program parameter hash
my %tools = ( 'program1' => { 'PARM1' => 1, 'PARM2' => 'n', 'PARM3' =>
+ 'y', 'PARM4' => 'n', 'PARM5' => 'y', 'PARM6' => 'n', 'PARM7' => 'y',
+ 'PARM8' => 'n', 'PARM9' => 'y', 'PARM10' => 'n' }, 'program2' => { '
+PARM1' => 1, 'PARM2' => 2, 'PARM3' => 3, 'PARM4' => 4, 'PARM5' => 5,
+'PARM6' => 6, 'PARM7' => 7, 'PARM8' => 8, 'PARM9' => 9, 'PARM10' => 1
+0 });
+
## create notebook
my $book = $mw->NoteBook()->pack(-expand=>1,-fill=>'both');
#####program1#######
my $tab_cnt=1;
for my $tab (keys %tools){
##create tab,scroll pane,frames
my $tab_tab=$book->add("Sheet $tab_cnt", -label => "$tab")->pack
+(-expand=>1,-fill=>'both');
my $tab_spane=$tab_tab->Scrolled('Pane',-background=>'slate grey
+')->pack(-expand=>1, -fill=>'both');
my $tab_frame1=$tab_spane->Frame(-background=>'blue')->pack(-sid
+e=>'left',-expand=>1,-fill=>'both',-padx=>15);
my $tab_frame2=$tab_spane->Frame(-background=>'red')->pack(-side
+=>'bottom',-anchor=>'s',-fill=>'x');
$tab_cnt++;
#create columns
my $tab_column1 = $tab_frame1->Frame()->pack(-side=>'left',-exp
+and=>1,-fill=>'both');
my $tab_column2 = $tab_frame2->Frame()->pack(-side=>'right',-ex
+pand=>1,-fill=>'both');
##now fill frames
my $parm_cnt=1;
print "Processing tool: $tab\n";
foreach my $parm ( keys %{$tools{$tab}}) {
print " $parm = $tools{$tab}{$parm}\n";
if ($parm_cnt < 6 ){
my $tab_test=$tab_column1->LabEntry(-label => "$parm=")
+;
$tab_test->Subwidget('entry')->configure(-textvariable => \$t
+ools{$tab}{$parm} );
$tab_test->configure(-labelPack=>[-side=>'left']);
$tab_test->pack(-anchor=>'e');
} else {
my $tab_test=$tab_column2->LabEntry(-label => "$parm=")
+;
$tab_test->Subwidget('entry')->configure(-textvariable => \$t
+ools{$tab}{$parm} );
$tab_test->configure(-labelPack=>[-side=>'left']);
$tab_test->pack(-anchor=>'e');
}
$parm_cnt++;
}
#######run button#############
my $run=$tab_tab->Button(-text => "\n $tab \n ", -command
+=> \&run,-background=>'slate grey')->pack;
}
MainLoop;
####################### subs #################################
sub run {
#print "Label = Entry" to file
#execute program
}
Also, how do I keep my hash in order? Tie::IxHash ?
Thank you for the help.
-honyok | [reply] [d/l] |
Now, I would like to capture any user changes/inputs and write a text file consisting of: LABEL=ENTRY\n LABEL=ENTRY\n .. So open a file, and loop thru your hash and print to the file. You can do this by adding code in the run callback, to open the file, loop thru your hash, print the keys values, then close the file. I will show how this is done in the script below, which shows how to sort your keys properly. The problem with sorting your keys, is that you have a mixed alpha-numeric string. if you just use sort on the keys, you will end up with the typical alpha order
1,10,2,3, etc. So you must split you keys into alpha and numeric fields, then sort on them. I show this below. Now sorting is a big topic, and I won't explain the details here, but you can see List Processing, Filtering, and Sorting. As you work thru it, save the examples as recipes for various sorts. So here is sort, and print to file.
#!/usr/bin/perl
use warnings;
use strict;
my %tools = ( 'program1' => { 'PARM1' => 1, 'PARM2' => 'n', 'PARM3' =>
+ 'y',
'PARM4' => 'n', 'PARM5' => 'y', 'PARM6'
+=> 'n',
'PARM7' => 'y', 'PARM8' => 'n', 'PARM9'
+=> 'y',
'PARM10' => 'n' },
'program2' => { 'PARM1' => 1, 'PARM2' => 2, 'PARM3' => 3
+, 'PARM4' => 4,
'PARM5' => 5, 'PARM6' => 6, 'PARM7' => 7
+, 'PARM8' => 8,
'PARM9' => 9, 'PARM10' => 10 });
open (my $oh,"> $0-settings.txt") or warn "$!\n";
foreach my $tab (sort keys %tools){
print $oh "\n$tab\n";
foreach my $parm (custom_sort(\%{$tools{$tab}})) {
print $oh " $parm = $tools{$tab}{$parm}\n";
}
}
close $oh;
#for my $key ( custom_sort(\%hash) ) {
# print "Key: $key\n";
#}
sub custom_sort {
my $hash = shift;
return
map { $_->[0] }
sort {
$a->[1] cmp $b->[1]
||
$a->[2] <=> $b->[2]}
map { [ $_, /(\w+)(\d+)$/ ] }
keys %$hash;
}
As far as detecting changes go, there are a few ways to do it. One way, is to make a callback for the -raisecmd of the notebook options. Google for examples. The idea is when a tab is selected (raised), the -raisecmd callback loops thru all the values on that page and stores them somehow.. You would need to store the previous tab, as well as the current tab. When a new tab is raised, the -raisecmd would loop thru all entries on the previous page, and compare them to the original. It is simple bookkeeping. If a difference is detected, do whatever. Another option, might be to try to use the validate callback of the entry widgets... it might be a simpler approach. If an attempt is made to an entry, a callback is triggered. That can flag that entry as "changed", whether you actually change the data or not. So it's really up to you and your program's needs, as to how you detect changes. The absolute simplest approach, is to maintain a separate hash of all the values, and on every -raisecmd, loop thru the backup hash, and compare the entries, and detect differences. It may waste a few cpu cycles, but it is a simple way.
| [reply] [d/l] |
Great! Lots of good info. I'll let you know how it works out.
-honyok
| [reply] |