Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

What is the best way to arrange widgets using TK?

by ~~David~~ (Hermit)
on Aug 24, 2004 at 03:16 UTC ( #385303=perlquestion: print w/ replies, xml ) Need Help??
~~David~~ has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a TK window that displays the keys and values of a hash. I would like to arrange the label and entry widgets dynamically depending on the size of the hash. I can't seem to find a good way to do it. Here is a snippet of my code where I tried to use GRID:
foreach (sort keys(%density)) { my $keytext = $mb->Label ( -height => 1, -width => 10, -text => "$_", -justify => 'left' ); $keytext -> grid( -column => "0", ); } foreach (sort keys(%density)) { my $denbox = $mb->Entry( -textvariable => \$density{$_}, -width => 5 ); $denbox -> grid( -column => "1" );
Can anyone help? Thanks, David

Comment on What is the best way to arrange widgets using TK?
Download Code
Re: What is the best way to arrange widgets using TK?
by Elijah (Hermit) on Aug 24, 2004 at 04:08 UTC
    Something like the following maybe?
    #!/usr/bin/perl -w use strict; use Tk; my %density = ( Some_Value => qw(Something), Another_Value => qw(Something_Else), Some_More => qw(Some_More), ); my $mb = MainWindow->new(); $mb->minsize(qw(200 100)); for (sort keys(%density)) { my $keytext = $mb->Frame->pack(-side => 'top', -fill => 'none', -exp +and => 1); $keytext -> Label(-text=>$_, -relief=>'ridge')->pack(-side=>'left', +-anchor=>'n'); $keytext -> Label(-text=>" ")->pack(-side=>'left', -anchor=>'n'); $keytext->Entry(-textvariable => \$density{$_},-width => 15)->pack(- +side=>'right', -anchor=>'n');; } MainLoop();

    www.perlskripts.com
      Thank you all for your help, I will definately use all or part of most of the responses here. I had another problem, and that is the fact that I have to display multiple columns of information. So I have a bunch of hashes that I need to display now. What I have done so far is to create frames and frames of frames, etc. Using this method, I can ->pack() everything in each frame. This is a little cumbersome, however, since I have about 12 frames now. Thanks again everyone.
Re: What is the best way to arrange widgets using TK?
by mawe (Hermit) on Aug 24, 2004 at 04:55 UTC
    Hi!

    You were close, but you forgot -row:

    use Tk; my $top = new MainWindow(); my %density = (A=>1,B=>2,C=>3); my $row = 0; foreach (sort keys(%density)) { my $keytext = $top->Label( -height=>1,-width=>10,-text=>$_,-justify=>'left')->grid(-row=> +$row,-column=>0); my $denbox = $top->Entry( -textvariable=>\$density{$_},-width=>5)->grid(-row=>$row,-colu +mn=>1); $row++; } MainLoop();
    mawe
Re: What is the best way to arrange widgets using TK?
by qumsieh (Scribe) on Aug 24, 2004 at 05:56 UTC
    Others have given you answers. I'll add some comments:

    1) You can use a Tk::LabEntry widget instead of separate Label and Entry widgets. The result is identical, but it's a bit shorter to code.
    2) The -justify option controls justification when there is more than one line of text, which is not the case in your case. What you really need is the -anchor option and set it to 'w'.
    3) To align all your text in the Label widgets, set the value of the -sticky option of grid() to 'ew' for each of your Label widgets.

    HTH.

      You can use a Tk::LabEntry widget instead of separate Label and Entry widgets. The result is identical, but it's a bit shorter to code.
      This is not identical, because a LabEntry widget occupies only one cell, be it packed or gridded. This means that you can't easily align a number of vertically stacked LabEntries. (There are workarounds, but it's always easier to create Label and Entry seperately and use the grid geometry manager)
Re: What is the best way to arrange widgets using TK?
by rinceWind (Monsignor) on Aug 24, 2004 at 06:35 UTC
    I think what you are after is using relative placement with the grid geometry manager. This is something that is not very well explained in the documentation IMHO.

    See the RPN calculator tutorial on the Perl/Tk wiki for a worked example.

    --
    I'm Not Just Another Perl Hacker

Re: What is the best way to arrange widgets using TK?
by eserte (Deacon) on Aug 24, 2004 at 08:13 UTC
    I like the procedural form of grid, so I don't have to care about -row and -column. Here's your sample rewritten:
    foreach (sort keys(%density)) { Tk::grid( $mb->Label( -text => $_, -anchor => "w", ), $mb->Entry( -textvariable => \$density{$_}, -width => 5 ), -sticky => "ew", ); }
      Interestingly, what Tk::grid sees with the "procedural form" of call, is identical to invoking the grid method on the leftmost widget, and passing in the remaining widgets as parameters.

      This is because a method call passes in the object as the first parameter.

      --
      I'm Not Just Another Perl Hacker

Re: What is the best way to arrange widgets using TK?
by Scarborough (Hermit) on Aug 24, 2004 at 15:35 UTC
    I agree with most of whats said above and in fairness they answer the question you ask but if you need to refer to the values in the text boxs and the labels else where in the program you need to look at RPN Calculator as surgested above it really helped me yesterday when I was trying to build a calender module.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://385303]
Approved by SciDude
Front-paged by Elijah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-08-23 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls