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

Hi everyone. I am creating a basic GUI using Perl Tk and i have a feature i want to implement that i was hoping i could get some assistance with. Basically i have a text box which we can enter some client IP. Now if I press Button "Add Host", Another text box should appear to enter host IP. This should happen everytime "Add Host" button is pressed. Im new to perl Tk. Please help.

  • Comment on Perl Tk - Add text box on each button event

Replies are listed 'Best First'.
Re: Perl Tk - Add text box on each button event
by Anonymous Monk on Aug 05, 2011 at 08:56 UTC
Re: Perl Tk - Add text box on each button event
by moritz (Cardinal) on Aug 05, 2011 at 08:50 UTC

    Which part are you having trouble with?

    You seem to know how to construct a GUI at startup, adding elements to it at runtime is not different than at startup. You can keep GUI elements in arrays, just like any other objects.

      But how many time Im going to press button and how many element can i keep it in array is unknown right? Please correct me if Im wrong here.

        You can keep as many elements in an array as you have memory for storage. So just add one every time the user presses the button. (And maybe remove one if the user presses a different button).

      I actually did not get the GUI element. This is my line of code $AddMoreTarget=$AddtargetBtnConfig->Button(-text=>"Add Host",-font=>"bold",-background =>"blue", -foreground => "white",-width=>20, -command=>\&AddHost, $AddtargetConfig1)->pack(); Where the "AddHost" will add the text box.Can you please point out the element which I can put into the array?

Re: Perl Tk - Add text box on each button event
by zentara (Cardinal) on Aug 06, 2011 at 09:48 UTC
    Im new to perl Tk. Please help.

    When you want to dynamically add a bunch of widgets, like a Text or Canvas, it's usually best to put them into a Scrolled Pane. Here is a simple example.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; my $mw = MainWindow->new; $mw->geometry('800x600+100+100'); $mw->fontCreate('big', -weight=>'bold', -size=> 18 ); my $count = 0; # hash to hold the text widgets my %text; my $abutton = $mw->Button( -text => 'Add New Text Widget', -bg => 'lightyellow', -command => \&add, -font => 'big' )->pack(-fill=>'x'); # Scrolled Pane to be the overall container my $sp = $mw->Scrolled('Pane', -scrollbars => 'osoe', )->pack(-fill=>'both', -expand=>1 ); # a simple text inserter to all text widgets my $repeater = $mw->repeat(2000,\&insert ); MainLoop; sub add{ my $num = $count++; # make a frame to lock in the scrolled text to the scrolled pane my $frame = $sp->Frame()->pack(-fill=>'x', -expand=> 1); $text{$num} = $frame->Scrolled('Text', -background=>'lightsteelblue', -foreground=>'black', -font => 'big', -height => 15, # how many lines are shown -width => 100, # how many characters per line )->pack(-fill=>'both', -expand=>1); } sub insert { my $data = rand 100000; foreach my $num (keys %text){ $text{$num}->insert('end',"text number $num-> ". $data); $text{$num}->insert('end',"\n"); $text{$num}->see('end'); } }

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