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

Hey,

I have going deep into this module since I'm customizing it for my own purposes... :) However, I'm not sure how to change the Button's (HButton) height! At row 150 I tried the following:
my $hdr = $f->HButton( -takefocus=>0, -padx=>0, -width=>1, -borderwidth=>2, -highlightthickness=>0 )->pack(qw/-side top -anchor n -fill x/); $hdr->GeometryRequest($hdr->reqwidth, 18); # Row 150, New! $hdr->update; # Row 151, New! $w->Advertise("heading" => $hdr);
But no luck. Well, the first buttons are changed, at starup, but not all. And the whole thing is too small. I also changed at around row 102:
if (defined(my $pixels = $w->cget('-pixelwidth'))) { $w->GeometryRequest($pixels, 18); # 18 is New! Was $mw->re +qheight before }
But here it's just changed when I click or drag. I want it to be set at startup! Testcode:
use Tk; use Tk::MListbox; use strict; use warnings; my $mw = new MainWindow(); my $frame = $mw->Frame(-bd => 2, -relief => 'raised')->pack(-expand => + 1, -fill => 'both'); my $scrolledMListbox = $frame->Scrolled( qw/MListbox -selectmode browse -scrollbars oe/ )->pack(-expand => 1, -fill => 'both'); my @list = ( "a", "b" ); $scrolledMListbox->columnInsert('end', -text => 'Path'); $scrolledMListbox->columnInsert('end', -text => 'Modified'); $scrolledMListbox->columnInsert('end', -text => 'Dir'); $scrolledMListbox->columnInsert('end', -text => 'File'); $scrolledMListbox->insert('end', [ "a", "b", "s", "l"] ); $scrolledMListbox->insert('end', [ "c", "d", "f", "e"] ); MainLoop;
Thanks,
Ace

Replies are listed 'Best First'.
Re: Changing Buttonsize in MListbox
by zentara (Cardinal) on Sep 15, 2005 at 23:10 UTC
    Buttons and Labels in Tk are text-line oriented. You can't really set the height or width in pixels, when you set the width, it's the character width of the text (at the font it's using). The height can only be changed by putting newlines in it's text. Here are 2 examples:
    #!/usr/bin/perl use Tk; use Tk::MListbox; use strict; use warnings; my $mw = new MainWindow(); my $frame = $mw->Frame(-bd => 2, -relief => 'raised')->pack(-expand => + 1, -fill => 'both'); my $scrolledMListbox = $frame->Scrolled( qw/MListbox -selectmode browse -scrollbars oe/ )->pack(-expand => 1, -fill => 'both'); my @list = ( "a", "b" ); $scrolledMListbox->columnInsert('end', -text => "\nPath\n"); $scrolledMListbox->columnInsert('end', -text => "\nModified\n"); $scrolledMListbox->columnInsert('end', -text => "\nDir\n"); $scrolledMListbox->columnInsert('end', -text => "\nFile\n"); $scrolledMListbox->insert('end', [ "a", "b", "s", "l"] ); $scrolledMListbox->insert('end', [ "c", "d", "f", "e"] ); MainLoop;
    or you can try to set a font size.
    #!/usr/bin/perl use Tk; use Tk::MListbox; use strict; use warnings; my $mw = new MainWindow(); $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size=> 18 ); $mw->fontCreate('medium', -family=>'arial', -weight=>'bold', -size=> 14 ); my $frame = $mw->Frame(-bd => 2, -relief => 'raised')->pack(-expand => + 1, -fill => 'both'); my $scrolledMListbox = $frame->Scrolled( qw/MListbox -selectmode browse -scrollbars oe -font medium / )->pack(-expand => 1, -fill => 'both'); my @list = ( "a", "b" ); $scrolledMListbox->columnInsert('end', -text => "Path",-font => 'big' +); $scrolledMListbox->columnInsert('end', -text => "Modified",-font => 'b +ig'); $scrolledMListbox->columnInsert('end', -text => "Dir",-font => 'big') +; $scrolledMListbox->columnInsert('end', -text => "File",-font => 'big' +); $scrolledMListbox->insert('end', [ "a", "b", "s", "l"] ); $scrolledMListbox->insert('end', [ "c", "d", "f", "e"] ); MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      Well, if you try that I mentioned I do make it thinner! But it's not working for them all buttons, and I dunno why! (well, atleast it's not working directly, when creating the MListbox!, which I want).

      Tried inserting my addon to the MListbox.pm? That "18" in height in pixels makes the button thin, when I click or drag in there...

        After you have created your MListbox, try adding the following code:

        foreach my $column ($mlistbox->columnGet(0, 'end')) { $column->Subwidget("heading")->configure(-pady => 0); }

        Sometimes, by tweaking the pady option you can get the height you want. Does this give the desired result?

        Rob
Re: Changing Buttonsize in MListbox
by InfiniteSilence (Curate) on Sep 15, 2005 at 22:39 UTC
    If I understand you right, all you want to do is to make the columns larger, right? Update: Are you looking to make the columns wider, or the buttons at the top taller (in which case I don't get it...they are just a bit larger than the text, which at least on Win32 is just like every other application)?

    ... $mw->minsize(700,400); ... my $c1 = $scrolledMListbox->columnInsert('end', -text => 'Path'); $c1->configure(-width => 25); my $c2 = $scrolledMListbox->columnInsert('end', -text => 'Modified'); $c2->configure(-width => 25); my $c3 = $scrolledMListbox->columnInsert('end', -text => 'Dir'); $c3->configure(-width => 25); my $c4 = $scrolledMListbox->columnInsert('end', -text => 'File'); $c4->configure(-width => 25);

    I am not certain whether you were thinking to modify the actual module just as an example or what, but normally you want to stay OUT of module code until you have exhausted other possibilities. Have you tried not hacking the module?

    Celebrate Intellectual Diversity

      I wanna make the buttons thinner (aka less height than standard, since I think they are abit too big). Width is ok (and modified with -size doing the columnInsert()).
Re: Changing Buttonsize in MListbox
by rcseege (Pilgrim) on Sep 16, 2005 at 18:15 UTC

    For Tk problems, it's often useful to know the platform and the Tk version that you're using. I know, for example, that the height of Buttons in Windows was noticable different than in Unix -- or at least it used to be. It had to do with how the y padding was done. On my Windows machine I ran a simple test script, and the Buttons were shorter than I recall which means that maybe something was done to fix this problem in recent versions.

    I used to work around this by rolling my own Button by subclassing Label and adding minimal Button-like bindings. This was one of the reasons why I chose to do this for JComboBox, as an example.

    GeometryRequest can be fiddly and I have a hard time recommending it, When you use it, you start to contradict what the geometry manager wants to do, and often you end up with a tug-of-war over control of the layout where a widget changes size in odd ways. I would also suggest that by setting a hard-coded value for the height of the Button, then you are potentially putting yourself at odds with whatever the user has designated as the desired height. If you force it to be shorter than the font, then some portion of the text will be clipped.

    As Zentara pointed out, the height of a Button or Label is determined primary by the font, but other factors contribute such as:

    • the -pady setting (pixels above and below the text)
    • -borderwidth setting
    • -highlightthickness setting

    Of these, typically the one that's causing the problem is the -pady setting. (In the other post, I provided one way of setting this for all the Headers).

    Another approach that can be used to constrain the height of a Button would be pack it inside a Frame, then disable pack propagation on that Frame. Now you can control the size of the Button through it's container, and specify a pixel value. As I mentioned before, this is generally *not* a good thing because you want the Button to be at least as tall as the font.

      Incidentally, I absolutely do not mind if you change the internals to fit your needs (It's not as though I could do anything about it, even if I did mind..), Hans initially created and released it so that it could useful - even if that use is to serve as a base for others to modify. Send me your changes if you like, and they may find their way into the codebase.

      I would ask, however, and again obviously I can't enforce it, that when you do make changes that you rename it something other than MListbox. It doesn't have to be completely different (MyMListbox, MListbox2, AceMListbox, etc) but enough to distinguish it from the original. Thanks!

      Rob