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

Dear monks, I'm trying to create TK application where some values (Entry widget) should be entered by a user. This values should have a description (Label widget) on right side of them. The problem is to have the description just after the corresponding Entry widget. I'm pretty shure I missed some important but can't find any advice in the docs how to manage this kind of positioning.
This is a picture I want:
-- |5 | Enter the number of daily log files to copy -- -- |5 | Enter the number of audit log files to copy --
This is what i get now:
-- |5 | Enter the number of + daily log files to copy -- -- |5 | Enter the number of audit log files to copy --

It look like the left border of the first Label starts where right border of the second label is. I tried to add some free space using ipadx and ipady statements to the pack operator but didn't managed to get appropriate result.
use Modern::Perl; use Tk; my %config = ( numberOfDailyLogsToCopy => 5, numberOfAuditLogsToCopy => 5, ); my $mainWindow = MainWindow->new( -title => "main window", ); my $chkboxFrame = $mainWindow -> Frame( -borderwidth => 2, ) +->pack( -anchor => 'w', ); my $entryDailyLogs = $chkboxFrame -> Entry( -textvariable => \$config{'numberOfDailyLo +gsToCopy'}, -width => 2, ) -> pack( -anchor => 'w',); my $entryDailyLogsText = $chkboxFrame -> Label( -text => 'Enter the number of daily log fi +les to copy', ) -> pack( -before => $entryDailyLogs, -si +de => 'right' ); my $entryAuditLogs = $chkboxFrame -> Entry( -textvariable =>\$config{'numberOfAuditLog +sToCopy'}, -width => 2, ) -> pack( -anchor => 'w', ); my $entryAuditLogsText = $chkboxFrame -> Label( -text => 'Enter the number of audit log fi +les to copy', ) -> pack( -before => $entryAuditLogs, -si +de => 'right'); MainLoop;

With an additional frame everything is works fine:
use Modern::Perl; use Tk; my %config = ( numberOfDailyLogsToCopy => 5, numberOfAuditLogsToCopy => 5, ); my $mainWindow = MainWindow->new( -title => "main window", ); my $dailyFrame = $mainWindow -> Frame( -borderwidth => 2, ) - +>pack(); my $entryDailyLogs = $dailyFrame -> Entry( -textvariable => \$config{'numberOfDailyLo +gsToCopy'}, -width => 2, ) -> pack( -side => 'left', -anchor => 'w' +); my $entryDailyLogsText = $dailyFrame -> Label( -text => 'Enter the number of daily log fi +les to copy', ) -> pack( -side => 'left' ); my $auditFrame = $mainWindow -> Frame( -borderwidth => 2, + ) ->pack(); my $entryAuditLogs = $auditFrame -> Entry( -textvariable =>\$config{'numberOfAuditLog +sToCopy'}, -width => 2, ) -> pack( -side => 'left', -anchor => 'w' + ); my $entryAuditLogsText = $auditFrame -> Label( -text => 'Enter the number of audit log fi +les to copy', ) -> pack( -side => 'left' ); MainLoop;

I'm just wondering if there is an easier possibility to adjust the labels without adding a frame for each pair of elements.
Thank you in advance.

Replies are listed 'Best First'.
Re: TK positioning of elements
by lamprecht (Friar) on May 21, 2010 at 12:37 UTC
    Hi,

    you can use Tk::LabEntry ( ahh - no pod ) like so:

    my $entryDailyLogs = $mainWindow -> LabEntry( -textvariable => \$config{'numberOfDailyLogsToCopy'}, -width => 2, -label => 'Enter the number of daily log files t +o copy', -labelPack => [-side => 'right'], ) -> pack( ); my $entryAuditLogs = $mainWindow -> LabEntry( -textvariable =>\$config{'numberOfAuditLogsToCopy'}, -width => 2, -label => 'Enter the number of audit log files t +o copy', -labelPack => [-side => 'right'], ) -> pack( );

    Cheers, Christoph
Re: TK positioning of elements
by Marshall (Canon) on May 21, 2010 at 14:02 UTC
    Adding a additional frames is the right idea!
    Pack is the most commonly used frame manager and is a good idea.

    I think that the thing that you are missing is that you can make a sub() that is a "gizmo factory". I made a simple re-formulation of your code below to demo this idea which produces the same output on my machine as your 2nd version of code. You can make a bit different thing than your %config and include the text to be displayed in that structure and of course modify the add_entry_window() subroutine. There is another post in this thread that describes a widget that does what you need for a labeled entry window. I would use the most simple widget possible.

    But what I'm trying to say is that if you wanted 50 of these sort of lines in the GUI, make maybe an AoA and call one sub in a loop that creates them all!

    use Tk; $numberOfDailyLogsToCopy = 5; $numberOfAuditLogsToCopy = 5; my $mainWindow = MainWindow->new( -title => "main window", ); add_entry_gizmo ( $mainWindow, \$numberOfDailyLogsToCopy, "Enter the number of daily log files to copy", ); add_entry_gizmo ( $mainWindow, \$numberOfAuditLogsToCopy, "Enter the number of audit log files to copy", ); MainLoop; sub add_entry_gizmo { my ($window, $text_var_ref, $label, ) = @_; my $frame = $window -> Frame( -borderwidth => 2, ) ->pack(-anchor =>'w'); $frame-> Entry( -textvariable =>$text_var_ref, -width => 2, )-> pack(-side => 'left' ); $frame -> Label( -text => $label, ) -> pack(-side => 'left' ); }
      Thank you!

      @Christoph: this what I was looking for. I tried to add a "Label" to an entry widget but failed :-( because it's not supported!

      @Marshall: At the moment I only need to this 2 elements but I hope to remember your solution in case I write more complex GUI. This is a really cool trick to abstract the creation of elements using a sub.

Re: TK positioning of elements
by Anonymous Monk on May 21, 2010 at 11:25 UTC