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

Hello. I have a perl/tk gui working to some degree of satisfaction, but this is my first attempt. I have successfully executed a program, however a problem occurs when the "program2" tab is selected and the window is resized. The tabs seem to disappear. Any ideas? -- The purpose is a simple front-end to several standalone programs which run from scripts. If there are any obvious flaws or pitfalls, please mention them.
#!/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"); ##################NOTES######## ##expansion from 2nd tab has bug? ###default hash tie my %tools, 'Tie::IxHash', 'program1' => ordered_hash_ref ( 'PARM1' => 'Y', 'PARM2' => 'files', ' +PARM3' => 'list', 'PARM4' => 'Y', 'PARM5' => 'N'), 'program2' => ordered_hash_ref ( 'PARM' => '', 'PARM0' => 0, 'PARM1' = +> 1, 'PARM2' => 2, 'PARM3' => 3, 'PARM4' => 4, 'PARM5' => 5, 'PARM6' +=> 6, 'PARM7' => 7, 'PARM8' => 8, 'PARM9' =>9, 'PARM10' => 10 ); + ## 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"; my $tab_test; if ($parm_cnt < 7 ){ $tab_test=$tab_column1->LabEntry(-label => "$parm=" +); $tab_test->Subwidget('entry')->configure(-textvariable => + \$tools{$tab}{$parm} ); $tab_test->configure(-labelPack=>[-side=>'left']); $tab_test->pack(-anchor=>'e'); } else { $tab_test=$tab_column2->LabEntry(-label => "$parm=" +); $tab_test->Subwidget('entry')->configure(-textvariable => + \$tools{$tab}{$parm} ); $tab_test->configure(-labelPack=>[-side=>'left']); $tab_test->pack(-anchor=>'e'); } $parm_cnt++; } #######go and save button############# my $run=$tab_tab->Button(-text => "\n $tab \n ",-command=> +\&go ,-command =>[\&save_parms,$tab],-background=>'slate grey')->pack +; } MainLoop; ####################### subs ############################### ## save parameters ########## sub save_parms { my $tab = shift; #print $tab; open (my $parfile,"> ${tab}_parms.txt") or warn "$!\n"; foreach my $parm ( keys %{$tools{$tab}} ) { print $parfile "$parm=$tools{$tab}{$parm}\n"; } close $parfile; } ###########execute program sub go { #if ( "$tab" eq "program1"){ #open STDOUT,">tmpfile" or die "Cannot open temp file"; #open STDERR, '>>&STDOUT'; #system 'program1.exe' or die "$!\n" #close STDOUT; #} } ## order hashes## sub ordered_hash_ref { tie my %hash, 'Tie::IxHash', @_; return \%hash; } #######conflict between save_parms output and save_history output? #sub save_history { #foreach my $tool (sort keys %tools){ # open (my $histfile,"> ${tool}.history") or warn "$!\n"; # foreach my $text (custom_sort(\%{$tools{$tool}})) { # print $histfile "$text=$tools{$tool}{$text}\n"; # } #close $histfile; # } #} ###########################
Thanks. honyok

Replies are listed 'Best First'.
Re: Tk gui bug
by zentara (Cardinal) on Jan 30, 2009 at 15:28 UTC
      The perl works in mysterious ways!?!?
        It's not Perl...it's Tk mucking up, that's why I concentrate on Gtk2 now. :-) I was toying with it, using all the tricks I know, but no go. There is the -dynamicgeometry option for the NoteBook, and it seems to address the problem, but it has no effect. Usually with the Notebook, when it acts up from tab to tab, you need to set a -raisecmd option to adjust the geometry (like a $mw->update) when a tab is raised...but it dosn't work in this case.

        I notice that you have alot of nested packing with anchors, and all the other packing options, that seem to be messing with the Scrolled Pane's scrollbars. To be honest, I would go back to the example I mentioned earlier, that dosn't crush the tabs, and try to work your data set into that model. Or switch to Gtk2. :-)


        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Tk gui bug
by Anonymous Monk on Jan 30, 2009 at 11:29 UTC
    I think it may be a bug due to pack(too many panes/frames...), so I would switch Tk::grid.