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

Perl Tk compound formatting

by CColin (Scribe)
on Jun 06, 2013 at 15:37 UTC ( #1037479=perlquestion: print w/ replies, xml ) Need Help??
CColin has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Have just started using Perl Tk and am creating a compound button widget. Am pretty much adapting the compound button creation example from "Mastering Perl Tk" as follows:

my $bttn = $mw->Button; my $c = $bttn->Compound; $c->Space(-height => 10); $c->Line; $c->Space(-width => 10); $c->Text(-text => "A name", -font => 'times 19'); $c->Image(-image => $bttn->Photo(-file => "an_image.png") ); $c->Space(-width => 10); $c->Line; $c->Space(-height => 10);

This code is run within a loop of names/ images and does indeed spit out a list of buttons that represent what is required - however I'd like to align all the image files to the right of each button and fix the button widths so that they appear visually aligned as if in a vertical column. (Clearly the text for "A name" is of variable length, which explains why they are not currently aligned but ragged).

I'm reading docs without much luck. Any ideas how to do this?

Comment on Perl Tk compound formatting
Download Code
Re: Perl Tk compound formatting
by thundergnat (Deacon) on Jun 06, 2013 at 18:13 UTC

    Something like this perhaps? (using built in bitmaps in lieu of png images)

    use strict; use warnings; use Tk; my %w; $w{mw} = MainWindow->new; for (qw/ this that a_longer_name X /){ my $bttn = $w{mw}->Button( -text => "$_ ", -font => 'times 19', -justify => 'right', -compound => 'right', -bitmap => 'warning', #-image => $w{mw}->Photo( -file => 'whatever' ), -anchor => 'e', -padx => 4, )->pack; push @{$w{buttons}}, $bttn; } $w{mw}->update; { my $width = 0; for (@{$w{buttons}}) { $width = $_->width if $_->width > $width; } for (@{$w{buttons}}) { $_->configure( -width => $width ); } } MainLoop;
      Question: Sorry to bother you again, but how to add a scrollbar to this since the number of buttons may disappear off the page. I see that you have aded a mainwindow and a child window (somehow). I have tried the following code to create a scrollbar, but it only appears in the main window where there are no buttons displayed, rather than the child window:
      my $scrollbar = $mw->Scrollbar( ); my $lb = $mw->Listbox(-yscrollcommand => ['set' => $scrollbar]); #Configure the Scrollbar to talk to the Listbox widget $scrollbar->configure(-command => ['yview' => $lb]); #Pack the Scrollbar first so that it doesn't disappear when we resize $scrollbar->pack(-side => 'right', -fill => 'y'); $lb->pack(-side => 'left', -fill => 'both');

      How to get this scrollbar to appear alongside the buttons that are listed in the child window?

        scrollbars are kind of PITA to setup, which is why there is  $parent->Scrolled( 'widgetname', ... widgetoptions ... )->pack see Tk::Scrolled

        You don't want to (can't actually) scroll the main window directly. You need to insert a scrolled Frame or Pane into the toplevel (main window) then pack your widgets inside that.

        use strict; use warnings; use Tk; use Tk::Pane; my %w; $w{mw} = MainWindow->new; $w{mw}->geometry('220x350'); $w{bframe} = $w{mw}->Scrolled( 'Frame', -scrollbars => 'oe' )->pack( -expand => 1, -fill => 'both' ); for (qw/ this that a_longer_name X cheese verb witty_retort superstiti +on A..Z whatever/){ my $bttn = $w{bframe}->Button( -text => "$_", -font => 'times 19', -justify => 'left', -compound => 'right', -bitmap => ('error','info','question','warning')[rand 4], -anchor => 'e', -padx => 4, )->pack; push @{$w{buttons}}, $bttn; } $w{mw}->update; { my $width = 0; for (@{$w{buttons}}) { $width = $_->width if $_->width > $width; } for (@{$w{buttons}}) { $_->configure(-width => $width); } } MainLoop;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (20)
As of 2014-09-30 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (369 votes), past polls