Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Is there some limit to the max num of widgets in perl tk?

by Eradicatore (Monk)
on Nov 21, 2007 at 05:24 UTC ( #652059=perlquestion: print w/replies, xml ) Need Help??
Eradicatore has asked for the wisdom of the Perl Monks concerning the following question:

I seem to have hit some limit somewhere around 4000 labels in Perl/Tk. I'm using ActivePerl 813 on windows with 2GB of RAM. Here is the code. When I run this, you see the labels start turning yellow and then after that they stop showing up. So it's like I've maxed out and can't make anymore frames or labels?
#!/bin/perl use Tk; use Tk::Checkbutton; use Tk::Button; use File::Basename; ### Globals $version=0.10; $num_tasks=100; $max_timeout=60; ################# START OF CODE ################## my $mw=MainWindow->new; $mw->title("my app: Version $version"); $mw->Label(-text=>"something\n\n")->pack; my $mf = $mw->fontCreate('tiny', -family=>'arial', -size=>int(1)); $main_data_f = $mw->Frame(-background=>'red')->pack(-anchor=>'w'); foreach $i (1..$num_tasks) { $main_data_task_f[$i] = $main_data_f->Frame()->pack(-side=>'left'); foreach $j (1..$max_timeout) { #next if ($j>$i); if ($j%2) { if ($i%2) { $test_bg = 'red'; } else { $test_bg = 'blue'; } } else { if ($i%2) { $test_bg = 'blue'; } else { $test_bg = 'red'; } } if ($cnt++>3880) {$test_bg = 'yellow'}; $main_data_time_f[$i][$j] = $main_data_task_f[$i]->Frame()->pac +k(-side=>'bottom'); $box[$i][$j] = $main_data_time_f[$i][$j]->Label(-font=>$mf, -ba +ckground=>$test_bg)->pack(); } } MainLoop;

Justin Eltoft

"If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Replies are listed 'Best First'.
Re: Is there some limit to the max num of widgets in perl tk?
by zentara (Archbishop) on Nov 21, 2007 at 12:36 UTC
    On linux, your code maxed out at 60.3 megs, worked but sluggish as heck. The problem is not a max-widgets problem, but slugishness in the event-loop. The event-loop has to keep track of all those label widgets. You will have much better performance with a Canvas, since it is just a single widget; but even in the example below, you can only make it go so fast. If you need max speed, you need to go to c.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Is there some limit to the max num of widgets in perl tk?
by perlfan (Curate) on Nov 21, 2007 at 05:49 UTC
    First, I don't know - but 4000 labels? Are you sure you don't want to use a text box for all of that instead? I am sure no one has ever tested Tk to this point, but you may have found a bug or just run into a memory limitation. 4000 labels?
      Well, yes, I should have explained my reason as well. And maybe this is a bad idea. I was going to use them not as actual labels, but instead as a sort of spectrum display. Similar to one used on music players, but my data is not audio in this case. Anyway, I need about 100 on the x axis and 60+ on the y. so that's more than 4000 labels. I did the same thing with buttons and it failed the same exact way. Any suggestions maybe for a different way to have a 60x100 grid of items? I suppose I could do some sort of curses/ascii display, but I was hoping to have it more tk-ish.

      Justin Eltoft

      "If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

        Well, now that I think about it, I could try vertical progress bars like this:
        $main_data2_f = $mw->Frame()->pack(-side=>'top'); $vprog = $main_data2_f->ProgressBar( -width => 6, -length => 400, -anchor => 's', -from => 0, -to => 60, -gap => 2, -blocks => 60, -colors => [0, 'green', 50, 'yellow' , 55, 'red'], -variable => \$percent_done )->pack(-anchor=>'s'); $percent_done=55;

        What about using Tk::Canvas instead of the labels?

        Best regards,
        perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."


        Update: ...and if I'd learn to read the entire thread before posting, I'd've seen that you already thought of a better hack, and zentara already provided a nice starting point to a nice way to do it. IGNORE THIS POST!

        * UGLY HACK ALERT * If you're offended by hacks, STOP READING NOW!

        I'm no UI guy, so I would cheat with a hack: Instead of making a grid of labels (I assume you're just changing their color to draw the spectrum?), you could make a simple linear array off 100 of them, and simply change their heights. This will let you get the job done with fewer labels, and fewer operations (rather than update 4000 labels, you need only update 100), so you could get a better repetition rate.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://652059]
Approved by ikegami
[< & >]: in democracies people get the politicians they deserve

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-12-15 18:42 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (442 votes). Check out past polls.