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

Hello Monks, I decided to use perl TK for my GUI. Here is the issue i am having. For example, how can i positon a lable. With the win32::GUI it had a specific directive being -pos0,5 etc. Is there a equilvant for perl TK? Example below
my $mw = new MainWindow; $mw->minsize(qw(600 700)); $mw->maxsize(qw(600 700)); my $emailLab = $mw -> Label(-text=>"Enter name:") -> pack(); MainLoop;

Replies are listed 'Best First'.
Re: Perl TK simple question
by keszler (Priest) on Oct 21, 2009 at 14:58 UTC
    Another option would be to use the Tk::place geometry manager, which has x,y placement:
    -x => location
    Location specifies the x-coordinate within the master window of the anchor point for $slave widget. The location is specified in screen units (i.e. any of the forms accepted by Tk_GetPixels) and need not lie within the bounds of the master window.

    -relx => location
    Location specifies the x-coordinate within the master window of the anchor point for $slave widget. In this case the location is specified in a relative fashion as a floating-point number: 0.0 corresponds to the left edge of the master and 1.0 corresponds to the right edge of the master. Location need not be in the range 0.0-1.0. If both -x and -relx are specified for a slave then their values are summed. For example, "-relx=>0.5, -x=-2" positions the left edge of the slave 2 pixels to the left of the center of its master.

    -y => location
    Location specifies the y-coordinate within the master window of the anchor point for $slave widget. The location is specified in screen units (i.e. any of the forms accepted by Tk_GetPixels) and need not lie within the bounds of the master window.

    -rely => location
    Location specifies the y-coordinate within the master window of the anchor point for $slave widget. In this case the value is specified in a relative fashion as a floating-point number: 0.0 corresponds to the top edge of the master and 1.0 corresponds to the bottom edge of the master. Location need not be in the range 0.0-1.0. If both -y and -rely are specified for a slave then their values are summed. For example, -rely=>0.5, -x=>3 positions the top edge of the slave 3 pixels below the center of its master.
Re: Perl TK simple question
by biohisham (Priest) on Oct 21, 2009 at 14:49 UTC
    There are many ways you can do that, Perl Tk has different geometry (ex. pack, grid, place...) managers. To get a better control while positioning widgets you can:
    • specify options for the Geometry manager to make the widget placed to a certain position.
    • you can also place the widgets to be aligned in frame widgets and then pack the frame widgets, this has superior control, like when you want to create menu options bars.

    #here are some of the options you can specify to pack. #learn about the different arguments they take.. #general explanatory non-functional code... $emailLab=$mw->label->pack(-anchor=>'',-ipadx=>'', -ipady=>'',-expand=>'', -fill=>'', -padx=>'', -pady=>'' );
    #using frame widgets, general explanatory non-functional code use Tk; $main=MainWindow->new; $frame=$main->Frame; $frame->pack(pack options) or $frame->place(options); $frame->Label()->pack(); MainLoop;
    I would strongly advice you to do the following:
    1. check the Tk documentation through "perldoc Tk"
    2. run the Tk command "widget" from your console to see more examples.
    3. Check Geometry Management


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Perl TK simple question
by Anonymous Monk on Oct 21, 2009 at 13:58 UTC
    You are using Tk::pack geometry manager, you can pack east, west, north, south ... maybe you want Tk::grid?
Re: Perl TK simple question
by markuhs (Scribe) on Oct 21, 2009 at 14:43 UTC
    You could use padding to control positioning:
    my $emailLab1 = $mw->Label(-text=>"Enter name:", -relief => 'ridge')-> +pack(-side => 'left', -padx => 10);

    (I added the relief, on order to make it visible.)