Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Array to scroll..

by tamaguchi (Pilgrim)
on Jul 27, 2006 at 13:29 UTC ( [id://564078]=perlquestion: print w/replies, xml ) Need Help??

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

I have written the following:
#!/usr/bin/perl -w use strict; use Tk; my @arr=(100, 200, 300, 400 , 500); my $maincolor='red'; my $mw = MainWindow->new; $mw->minsize(qw(250 200)); #x y $mw->configure(-title=>'PROGRAM', -background=>$maincolor); my $frame_6=$mw->Frame(-borderwidth => 3, -background => $maincolor)-> +pack( -side=>'top', -fill=>'x'); $frame_6->Label(-text => 'Scroll', -background=>'yellow',)->pack(-side + =>'left'); my $scroll=$mw->Scrolled("Text", -scrollbars => 'e', -font => 'normal' +, -width => 10, -height => 10,)->pack; MainLoop;
I wonder now: How is it possible to make the values from array @arr automaticaly appear in the scrollwindow when the program is run? I would be thankfull for any help.

Replies are listed 'Best First'.
Re: Array to scroll..
by zentara (Archbishop) on Jul 27, 2006 at 13:38 UTC
    #!/usr/bin/perl -w use strict; use Tk; my @arr = ( 1..500 ); my $maincolor = 'red'; my $mw = MainWindow->new; $mw->minsize( qw(250 200) ); #x y $mw->configure( -title => 'PROGRAM', -background => $maincolor ); my $frame_6 = $mw->Frame( -borderwidth => 3, -background => $maincolor ) ->pack( -side => 'top', -fill => 'x' ); $frame_6->Label( -text => 'Scroll', -background => 'yellow', ) ->pack( -side => 'left' ); my $scroll = $mw->Scrolled( "Text", -scrollbars => 'e', -font => 'normal', -width => 10, -height => 10, )->pack; &loadarray( @arr ); MainLoop; sub loadarray{ my @array_in = @_; foreach my $element(@array_in){ $scroll->insert('end', "$element\n"); } }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thank you very much for your help. This makes me wonder another thing: Suppose that values have been entered to the scroll manually is there a way to get them out as an array?
Re: Array to scroll..
by reneeb (Chaplain) on Jul 27, 2006 at 13:51 UTC
    Do you want to show the values in the Tk::Text widget?

    #!/usr/bin/perl -w use strict; use Tk; my @array=(100, 200, 300, 400 , 500); my $maincolor='red'; my $mw = MainWindow->new; $mw->minsize(qw(250 200)); #x y $mw->configure(-title=>'PROGRAM', -background=>$maincolor); my $frame_6=$mw->Frame(-borderwidth => 3, -background => $maincolor)-> +pack( -side=>'top', -fill=>'x'); $frame_6->Label(-text => 'Scroll', -background=>'yellow',)->pack(-side + =>'left'); my $scroll=$mw->Scrolled("Text", -scrollbars => 'e', -font => 'normal' +, -width => 10, -height => 10,)->pack; for(0..scalar(@array)-1){ $scroll->insert($_,$array[$_]); } MainLoop;
Re: Array to scroll..
by rinceWind (Monsignor) on Jul 27, 2006 at 13:55 UTC

    Granted it's possible to prepopulate the Tk::Text widget as zentara has shown.

    My thoughts are that perhaps you should be using a scrolled Tk::Listbox object, which has a tied interface that you can use with your @arr, if you want changes to @arr to appear in the box automagically.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      The scroll is actually an scrolled entry box. I would like to get the values out of the scrolled entrybox by like it is possible to get the value out of a normal entry box with the method "get". Is there any equivalent method that could be used to get the values out from the entry box?
        I've never heard of a scrolled entry box, but maybe this is what you are looking for with a scrolled Listbox. It seems that you should read a book on Tk, these questions are very simple.
        #!/usr/bin/perl use warnings; use strict; use Tk; # Main Window my $mw = MainWindow->new; # Frames my $tf_frame = $mw->Frame->pack(); my $f_frame = $mw->Frame->pack(); my $bf_frame = $mw->Frame->pack(); # File Listing Label $tf_frame->Label(-text => "Select Items") ->pack(-side => 'left'); my @items = (1..500); my $listbox = $f_frame->Scrolled("Listbox", -scrollbars => "oe", -selectmode => "extended")->pack; $listbox->insert('end', @items); $bf_frame->Button(-text => "Done", -command => sub { &print_names($listbox); exit; })->pack; MainLoop; sub print_names{ my $listbox = shift; my @selected_items = $listbox->curselection; for (@selected_items) { my $item = $listbox->get($_); print "$item\n"; } }

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://564078]
Approved by Hue-Bond
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: (7)
As of 2024-04-19 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found