Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Binding columns in Tk::Hlist

by jdtoronto (Prior)
on Oct 05, 2006 at 01:51 UTC ( [id://576455]=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed monks,

In the following Perl/Tk code I want the second column to be the same width as the resizable button above it. Any suggestions?

#!/usr/bin/perl -w use strict; use Tk; use Tk::HListplus; my $mw = MainWindow->new(); # CREATE HEADER STYLE 1 my $headerstyle1 = $mw->ItemStyle( 'window', -anchor => 'nw', -pady => -10, -padx => 10, ); # CREATE MY HLIST my $hlist = $mw->Scrolled( 'HListplus', -scrollbars => 'oe', -columns => 2, -header => 1, -width => 40, -headerstyle => $headerstyle1, )->pack( -side => 'left', -expand => 'yes', -fill => 'both' ); # CREATE HEADER STYLE 2 my $headerstyle = $hlist->ItemStyle( 'window', -anchor => 'nw', -pady => 0, -padx => 10, ); $hlist->header( 'create', 0, -itemtype => 'resizebutton', -style => $headerstyle, -text => 'Test Name', -activebackground => 'white', ); $hlist->header( 'create', 1, -itemtype => 'resizebutton', -style => $headerstyle, -text => 'Status', -activebackground => 'white', ); my $red = $hlist->ItemStyle( 'text', -foreground => '#800000', -backgr +ound => 'white' ); my $blue = $hlist->ItemStyle( 'text', -foreground => '#000080', -ancho +r => 'e', -background => 'white' ); foreach ( [ Joe => '$10,000' ], [ Peter => '$20,000' ], [ Raj => '$90, +000 something to make it very much wider' ], [ Zinh => '$0' ] ) { my $e = $hlist->addchild(""); $hlist->itemCreate( $e, 0, -itemtype => 'text', -text => $_->[0], -s +tyle => $red ); $hlist->itemCreate( $e, 1, -itemtype => 'text', -text => $_->[1], -s +tyle => $blue ); } Tk::MainLoop;

jdtoronto

Replies are listed 'Best First'.
Re: Binding columns in Tk::Hlist
by rcseege (Pilgrim) on Oct 05, 2006 at 02:26 UTC

    Yes, but you're probably not going to like it. Without hacking the C source code, the best workaround I've found in the past is to add 1 more column than you expect to have within the HList. The end result is close to what you will see on similar Windows controls.

    Set -columns to 3, and ignore that extra column when populating the HList.

    Update: Also try setting -padx values in the styles to 0.

    Update Again: Ok, the only problem I see of adding that additional column (that you don't intend to use), is the fact that you'd probably like to be able to resize column 1, now, but you can't. The author has set the widget up so that the last header you create will not be allowed to resize -- I can see how this makes a kind of sense, assuming that you are always going to make resizable headers for all of our headers.

    A better approach would have been to check the total number of columns defined for the HList and to determine which column the resizebutton was being created for before deciding whether or not to allow the resize bar. This can be fixed easily by going to line 362 or so within the source, and just after the comment:

        # Create a new Resize Button

    Add the following two lines:

    my $lastCol = 0; $lastCol = 1 if $this->cget(-columns) == ($column + 1);

    And then, a few lines below where the HeaderResizeButton is being created change the -lastcolumn option to:

    -lastcolumn => $lastCol,

    I'll submit the patch to the Author and see if he'll take it. That's it for updates to this node...

    Rob
Re: Binding columns in Tk::Hlist
by Khen1950fx (Canon) on Oct 05, 2006 at 06:53 UTC
    I liked the suggestion by rcseege; but, when I read what you wanted to do, I thought of Tk::Columns---it's a multicolumn list widget that enables sorting and sizing of columns. It's packaged with Tk::DKW.

    If you want the second column to be the same width as the resizable button above it, then Tk::Columns has an event handler that seems up to the job:

    use Tk::Columns; if (defined ($p_Slave)) { $p_Slave->GeometryRequest ($this->reqwidth(), $p_Slave->reqheight( +)); $p_Slave->update(); $this->update(); }

    It will "slave the listbox to the current width of the button". I hope this helps.

      Using Tk::Columns (or Tk::MListbox) is a good suggestion, since they are both superior to Tk::HList in terms of performance, efficiency, and out-of-the-box functionality when it comes to tabular display.

      Unfortunately, both have one giant strike against them. Both use Listbox as part of their Column representation which has been known to leak in recent versions of Tk. This tends to be most obvious during sorting operations in both megawidgets since each completely delete, then reload all entries within each Listbox during a sort. Otherwise, I'd wholeheartedly recommend either one.

      HList becomes a lot more attractive when you need to do things like embedding other widgets, and have styles per cell. Although it is technically possible to have styles in the most recent version of Listbox, the options are more limited and neither Columns or MListbox have methods that provide easy access to this capability.

      A few last thoughts about HListplus:

      I've never seen the HListplus widget before now, but it would have been nice if each header provided a callback option that would be invoked after every resize (it doesn't). It would allow you to define a text style for each column and then modify the wraplength for each column as it's header resized from within the callback.

      Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found