in reply to Perl Tk - Add text box on each button event
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'); } }
|
|---|