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

I'm looking for a standalone separator widget for Perl/Tk, something to draw a line between other widgets. Imagine something like HTML's "hr" tag that can be horizontal or vertical ... or for those familiar with the old Motif, something like XmSeparator.

Does anyone know of an existing widget for this? If not, what could easily be made to do this?

I can do an empty Frame with a raised relief and a non-zero borderwidth like:
my $frame = $parent->Frame(); my $info = $frame->Label(-text => "General Info")->pack(); # separator ... sort of $frame->Frame(-background => "black", -borderwidth => 1, -relief => 'r +aised', -height => 2) ->pack(-fill => 'x', -padx => 5, -pady => 5); my $comments = $frame->Label(-text => "next widget")->pack();
which gets me really close, but it seems cumbersome. I assume I can do something similar for verticals with some option changes, but I haven't tried it yet. Is there a better alternative anyone can suggest?

Replies are listed 'Best First'.
Re: Looking for a standalone separator for Perl/Tk
by tybalt89 (Monsignor) on Mar 24, 2017 at 18:08 UTC

    Just use a simple black (or desired color) frame.

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1185809 use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->Label( -text => 'top part', -height => 5, )->pack; $mw->Frame( -height => 2, -bg => 'black', )->pack( -fill => 'x' ); $mw->Label( -text => 'bottom part', -height => 5, )->pack( -side => 'bottom'); $mw->Frame( -height => 2, -bg => 'black', )->pack( -side => 'bottom', -fill => 'x' ); $mw->Label( -text => 'left part', -height => 5, -padx => 25, )->pack( -side => 'left'); $mw->Frame( -width => 2, -bg => 'black', )->pack( -side => 'left', -fill => 'y' ); $mw->Label( -text => 'right part', -height => 5, -padx => 25, )->pack( -side => 'left'); MainLoop;
      Thanks! It's good to know I was on a reasonable path.

      I just wished there was something simpler like:
      use Tk::Separator; $mw->Separator(-orient => 'horizonal')->pack(); # -or- $mw->Separator(-orient => 'vertical')->pack();
      and all of the little details would automatically be taken care of. :) I suppose I could look at writing such a thing. Hmm, maybe I could steal some code from the Menubar or PanedWindow widgets since they have something like that already and make it its own widget.

        Like this ?

        #!/usr/bin/perl # http://perlmonks.org/?node_id=1185809 use strict; use warnings; use Tk; sub Tk::Separator { my ($self, %rest ) = @_; my $direction = delete $rest{'-orient'} // 'horizontal'; $self->Frame( %{ {%rest, -bg => 'black', $direction eq 'vertical' ? '-width' : '-height' => 2 } } ); } my $mw = MainWindow->new; $mw->Label( -text => 'top part', -height => 5, )->pack; $mw->Separator()->pack( -fill => 'x' ); $mw->Label( -text => 'bottom part', -height => 5, )->pack( -side => 'bottom'); $mw->Separator()->pack( -side => 'bottom', -fill => 'x' ); $mw->Label( -text => 'left part', -height => 5, -padx => 25, )->pack( -side => 'left'); $mw->Separator( -orient => 'vertical')->pack( -side => 'left', -fill = +> 'y' ); $mw->Label( -text => 'middle part', -height => 5, -padx => 25, )->pack( -side => 'left'); $mw->Separator( -orient => 'vertical')->pack( -side => 'left', -fill = +> 'y' ); $mw->Label( -text => 'right part', -height => 5, -padx => 25, )->pack( -side => 'left'); MainLoop;