Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: TK Gui Help

by zentara (Archbishop)
on Jun 27, 2014 at 15:51 UTC ( [id://1091490]=note: print w/replies, xml ) Need Help??


in reply to Re^4: TK Gui Help
in thread TK Gui Help

You sort of have the right idea, but there is one thing to watch out for: do not try to use the widget as a hashkey, as it will result in an error. You are doing it right by assigning a counter. Here is an example based on my code example above. P.S. To make your code look better, put your listboxes into Scrolled Panes. This example needs some work to make it look right, but it demonstrates the hash idea quite nicely.
#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry(q/+100+100/); $mw->title("Test Script"); #need exportselection=>0 for using multiple selection boxes my %lbhash; # you want to use the listbox to hold your # data, so store the widget's actual object # with the name you want to give it my @choices = ('afg', 'dfg', 'erty', 'etc'); foreach my $choice(@choices){ my $pane = $mw->Scrolled('Pane')->pack(-side => 'left'); my $label = $pane->Label(-text=> $choice, -bg=>'lightseagreen') ->pack(-side=>'top'); $lbhash{ $choice }{'widget'} = $pane->Listbox(-bg => 'white', -selectmode => 'extended', -exportselection=> 0 )->pack(-side=>'bottom'); my @random_data; for(1..20){push @random_data ,int rand(100) } $lbhash{ $choice }{'widget'} ->insert( 'end', @random_data ); } $mw->Button( -text => "Selections", -command => sub{ foreach my $key( keys %lbhash ){ my @selects = $lbhash{ $key }{'widget'}->curselection; print "$key: "; foreach my $select (@selects){ print $lbhash{ $key }{'widget'}->get($select),' '; } print "\n\n"; } } )->pack( -side => 'left' ); $mw->Button( -text => "Done", -command => \&exit )->pack; MainLoop;

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

Replies are listed 'Best First'.
Re^6: TK Gui Help
by Anonymous Monk on Jun 29, 2014 at 15:22 UTC
    My Gui is starting to come around, yet I still do not know how to add multiple units of the same widgets row-by-row and then to collect the data en-masse row-by-row. I have been able to pick up all the data from just one row. I am almost there. Here is the code:
    #!C:\Perl64\bin\perl.exe use strict; use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry(q/+100+100/); $mw->title("Test Script"); $mw->fontCreate('big', -weight=>'bold', -size=> 18 ); #need exportselection=>0 for using multiple selection boxes my %lbhash; # you want to use the listbox to hold your # data, so store the widget's actual object # with the name you want to give it my @choices = ('afg', 'dfg', 'erty'); my @divisional_unit =('Division Unit Left','Division Unit Middle','Div +ision Unit Right'); my $text=""; my $count = 0; my $num=$count++; my $count1 = 0; my $num1=$count1++; my $count2 = 0; my $num2=$count2++; my $count3 = 0; my $num3=$count3++; my @labels=("Left Text Header", "Middle Text Header", "Right Text Head +er"); my @labels1=("LC", "MC", "RC"); my @list = ("LC", "MC", "RC"); my %entry; my $inbox; my %checkbox; my $settings; foreach my $choice(@divisional_unit){ my $pane = $mw->Scrolled('Pane')->pack(-side => 'left'); #create a has for the entry widget $entry{$labels[$num++]} = $mw->Entry(-textvariable=>$text)->pack(-side +=>'left'); my $checkbox = $mw -> Checkbutton(-text=>$labels1[$num2++], -variable=>\$settings->{checkbuttons}->{$list[$num3++]},-offvalue= +>0,-onvalue=>1)->pack(-side=>'left'); my $label = $pane->Label(-text=> $choice, -bg=>'lightseagreen') ->pack(-side=>'top'); $lbhash{ $choice }{'widget'} = $pane->Listbox(-bg => 'white', -selectmode => 'extended', -exportselection=> 0 )->pack(-side=>'bottom'); #my @random_data; #for(1..20){push @random_data ,int rand(100) } $lbhash{ $choice }{'widget'} ->insert( 'end', @choices); #} } $mw->Button( -text => "Selections", -command => sub{ foreach my $key( sort keys %lbhash ){ my @selects = $lbhash{ $key }{'widget'}->curselection; print "$key: "; foreach my $select (@selects){ print $lbhash{ $key }{'widget'}->get($select),' '; } print "\n\n"; } foreach my $key1( sort keys %entry ){ my @selects2 = $entry{$key1}->get(); print "$key1:"; foreach my $s (@selects2) { print $s,"\n"; } } print join( "\n", grep { $settings->{checkbuttons}->{$_} +} @list ); } )->pack( -side => 'bottom' ); $mw->Button( -text => "Done", -command => \&exit )->pack(-side=>'botto +m'); MainLoop;
    Thanks for all the wonderful help zentara.

      My Gui is starting to come around, yet I still do not know how to add multiple units of the same widgets row-by-row and then to collect the data en-masse row-by-row. I have been able to pick up all the data from just one row. I am almost there. Here is the code:

      Why don't you know?

        For example, if I place the Gui code in a for loop that does two iterations as follows:
        use strict; use warnings; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry(q/+100+100/); $mw->title("Test Script"); my %lbhash; # you want to use the listbox to hold your # data, so store the widget's actual object # with the name you want to give it my @choices = ('afg', 'dfg', 'erty'); my @divisional_unit =('Division Unit Left','Division Unit Middle','Div +ision Unit Right'); my $text=""; my $count = 0; my $num=$count++; my $count1 = 0; my $num1=$count1++; my $count2 = 0; my $num2=$count2++; my $count3 = 0; my $num3=$count3++; my @labels=("Left Text Header", "Middle Text Header", "Right Text Head +er"); my @labels1=("LC", "MC", "RC"); my @list = ("LC", "MC", "RC"); my %entry; my $inbox; my %checkbox; my $settings; for (1..2) { foreach my $choice(@divisional_unit){ my $pane = $mw->Scrolled('Pane')->pack(-side => 'left'); #create a has for the entry widget $entry{$labels[$num++]} = $mw->Entry(-textvariable=>$text)->pack(-side +=>'left'); my $checkbox = $mw -> Checkbutton(-text=>$labels1[$num2++], -variable=>\$settings->{checkbuttons}->{$list[$num3++]},-offvalue= +>0,-onvalue=>1)->pack(-side=>'left'); my $label = $pane->Label(-text=> $choice, -bg=>'lightseagreen') ->pack(-side=>'top'); $lbhash{ $choice }{'widget'} = $pane->Listbox(-bg => 'white', -selectmode => 'extended', -exportselection=> 0 )->pack(-side=>'bottom'); #my @random_data; #for(1..20){push @random_data ,int rand(100) } $lbhash{ $choice }{'widget'} ->insert( 'end', @choices); #} } } $mw->Button( -text => "Selections", -command => sub{ foreach my $key( sort keys %lbhash ){ my @selects = $lbhash{ $key }{'widget'}->curselection; print "$key: "; foreach my $select (@selects){ print $lbhash{ $key }{'widget'}->get($select),' '; } print "\n\n"; } foreach my $key1( sort keys %entry ){ my @selects2 = $entry{$key1}->get(); print "$key1:"; foreach my $s (@selects2) { print $s,"\n"; } } print join( "\n", grep { $settings->{checkbuttons}->{$_} +} @list ); } )->pack( -side => 'bottom' ); $mw->Button( -text => "Done", -command => \&exit )->pack(-side=>'botto +m'); MainLoop;
        and proceed press the Selection button, the program will only capture the data from the second iteration and not the first. I also get the following message from the debugger:
        Use of uninitialized value within @labels in hash element at C:\Users\ +Ettore Vecchione\Desktop\FAO ORG CHART APP\grid3.pl line 58. Use of uninitialized value within @list in hash element at C:\Users\Et +tore Vecchione\Desktop\FAO ORG CHART APP\grid3.pl line 60. Use of uninitialized value within @labels in hash element at C:\Users\ +Ettore Vecchione\Desktop\FAO ORG CHART APP\grid3.pl line 58. Use of uninitialized value within @list in hash element at C:\Users\Et +tore Vecchione\Desktop\FAO ORG CHART APP\grid3.pl line 60. Use of uninitialized value within @labels in hash element at C:\Users\ +Ettore Vecchione\Desktop\FAO ORG CHART APP\grid3.pl line 58. Use of uninitialized value within @list in hash element at C:\Users\Et +tore Vecchione\Desktop\FAO ORG CHART APP\grid3.pl line 60.
        The array variables the debugger is reporting are uninitialised and are at the top of the program. See previous post in thread (i.e my @list,@labels). What should I be doing to correct the problem? Is it a problem with local vs. global scoping or is it a garbage collection issue? Thanks again.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found