in reply to Problem with Tk::Repeat

The main thing to remember with Tk (or any GUI coding), is that you start out by creating a bunch of widgets, then you start the program running and the widgets need to respond to things that happen (events). This model is referred to as 'event driven programming'. It's a bit different than writing console apps, and takes some getting used to.

Creating and destroying widgets in Tk leads to memory leakage. You need to keep your widgets around and reuse them instead of destroying them.

I noticed you had "use Tk::ProgressBar" in your example code. I've had big problems with memory leaks using ProgressBar widgets with Tk 804.027. YMMV.

I've hacked at your code a bit to make it work and refashion it a bit after my own style. There are things I'd still change (like naming things by purpose rather than 'Box_One'), but it works and should provide a good starting point.

#!Perl #$^W = 1; # is there a reason why you don't just use warnings? use strict; use warnings; use Tk; use Tk::BrowseEntry; use Cwd; use List::Util qw(shuffle); #Declarations# my $VERSION = 2.7; my $loadhistory = 0; my $sort_cnt = 3; my ($ftp, $port, $after_id,); my $cwd = cwd; my $mw = MainWindow->new( -relief => 'raised', -bd => 2, ); $mw->title("Asociacion de Agentes Aduanales de Matamoros --- Validacio +n Automatica"); $mw->geometry("1024x764+4+25"); # Make all your widget groups my $entry = Build_User_Entry($mw); my $dd = Build_Dropdown($mw); my $box1 = Build_Listbox_One($mw); my $box2 = Build_Listbox_Two($mw); my $button = Build_Exit_Button($mw); # Lay them out - use only one geometry manager per containing frame. $entry->grid( -columnspan => 2 ); $dd->grid( -columnspan => 2 ); $box1->grid( $box2 ); $button->grid( -columnspan => 2 ); # Start monitoring my ($enviados, $respuestas) = Start_Directory_Monitors( $mw, $box1, $b +ox2 ); MainLoop(); ################################################### # Build widgets sub Build_User_Entry { my $mw = shift; my $f = $mw->Frame(); my $label_wert = $f->Label( -text => 'Tu respuesta :', )->pack( -side => 'left', ); my $entry_wert = $f->Entry( -width => 20, )->pack( -expand => 1, -fill => 'y', ); return $f; } sub Build_Dropdown { my $mw = shift; my $f = $mw->Frame(); my $label_text = "nothing selected"; my $dropdown_value; my $showlabel = $f->Label( -textvariable => \$label_text, ); my $dropdown = $f->BrowseEntry( -label => "Label", -variable => \$dropdown_value, -browsecmd => sub { $label_text = "Ha seleccionado: $dropdown_value"; }, ); # Populate dropdown with values foreach ( qw/Enviados Recibido Errores/ ) { $dropdown->insert('end', $_); } # Set the initial value for the dropdown $dropdown_value = "Enviados"; $showlabel->pack; $dropdown->pack; return $f; } sub Build_Listbox_One { my $mw = shift; my $box1 = $mw->Scrolled( 'Listbox', -scrollbars => 'e', -relief => 'sunken', -height => 5, -setgrid => 0, ); return $box1; } sub Build_Listbox_Two { my $mw = shift; my $box2 = $mw->Scrolled( 'Listbox', -scrollbars => 'e', -relief => 'sunken', -height => 5, -setgrid => 0, ); return $box2; } sub Build_Exit_Button { my $mw = shift; my $button = $mw->Button( -text => "Salir", -command => sub { exit }, ); return $button; } ############################################################## sub Start_Directory_Monitors { my $mw = shift; my $enviados_display = shift; my $respuesta_display = shift; my $env = $mw->repeat(5000, [ \&list_dir_enviados, $enviados_disp +lay ] ); my $res = $mw->repeat(5000, [ \&list_dir_respuesta, $respuesta_dis +play ] ); return $env, $res; } # monitor enviados sub list_dir_enviados { my $box = shift; $box->delete(0,'end'); # empty listbox #my @items = glob "c:/AAAvalida/valida/enviados/*.*"; my @items = shuffle qw(One Two Three Four Five Six Seven Eight Nin +e Ten Eleven Twelve); foreach (@items) { $box1->insert('end', $_); } } # monitor respuestas sub list_dir_respuesta { my $box = shift; $box->delete(0,'end'); # empty listbox #my @items = glob "c:/AAAvalida/valida/respuestas/*.*"; my @items = shuffle qw(One Two Three Four Five Six Seven Eight Nin +e Ten Eleven Twelve); foreach (@items) { $box->insert('end', $_); } } ###################################################################### +###


TGI says moo

Replies are listed 'Best First'.
Re^2: Problem with Tk::Repeat
by padawan_linuxero (Scribe) on May 06, 2008 at 23:03 UTC
    Thank you all for helping me this is a better way
    but one question :
    how can I make the listbox bigger? because they are to small right now

    thanks

      You can adjust the size of the listbox by configuring its -height and -width.

      You can also allow the geometry manager to control a widget's size. For pack, you set -fill and -expand options when you pack the widget. For grid, you set the -sticky option to cause the widget to grow to fill the grid cell. You can also use gridRowconfigure and gridColumnconfigure with the -weight option to make a row/column expand as a window is resized.

      Here's a snippet you can paste in to replace line 38 of the code above:

      $box1->grid( $box2, -sticky => 'ns' ); $mw->gridRowconfigure(2, -weight => 1 );

      It's worth the time to really dig into and understand the pack and grid geometry managers. I've played with the other managers, but have never needed anything else for a real project.


      TGI says moo

      Thats why pack is the preferred geometry manager. You can use
      my $lb=$mw->Scrolled('Listbox')->pack(-fill=>'both', -expand=>1);
      and the listbox will grow in size to fill as much space as it can. It will auto-adjust size too, if you resize the window.

      Learning to use pack is definitely worth the time..... it takes a Saturday afternoon of time to get used to it's behavior, but it saves time and headaches in the long run. Grid has a more complex method called "weight".... but I always use pack.


      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        I kinda understand the line but where should I put it in my program ??