Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

TK listbox escape text

by jbuck (Novice)
on Jun 26, 2015 at 05:29 UTC ( [id://1132062]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

This seemingly simple problem frustrated me enough to sign up as a new user just to ask your opinions! I am no stranger to perl, but am a beginner incorporating TK into my scripts...

My problem is that I have a TK listbox which I am using to display search results of a database. The catch is that each result contains a "short title" and a "description", each of varying lengths. I want each line in the listbox to display the short title followed by the description. I can easily do this with various forms of concatenation, however the differing character lengths make for a "jagged" and ugly listing.

The obvious answer would be to use a tab (\t) in between, but the listbox doesn't seem to recognize escape sequences and instead displays their regex (xism?) equivalent?? I have looked everywhere for some listbox-specific tab, but as it doesn't use TK::text as a base I don't think it supports insertTab()? I don't see this problem when using ROText.

EXAMPLE:

my $text1 = "cherry\tred fruit"; my $text2 = "carrot\torange vegetable"; $lb->insert('end', $text1, $text2);
displays (literally):
cherry\x{9}red fruit carrot\x{9}orange vegetable
What is the "\x{9}"?? How do I use escape sequences / regex with listbox?? Thanks!!

Thanks for the input folks. sprintf was a good idea, but no luck; it still printed the \x{9} equivalent. I had briefly read about HList, but didn't want to go through the trouble of changing the widget. I think I'll try that now.

Until then, I have temporarily kludged it for our users haha:

my $string1 = "cherry"; my $string2 = "a red fruit"; my $spaces = 30 - length($string1); my $text = sprintf("%-${spaces}s\%s\%s",$string1,$string2);

Replies are listed 'Best First'.
Re: TK listbox escape text
by johngg (Canon) on Jun 26, 2015 at 09:12 UTC

    You could format your two fields into one string with sprintf then insert that.

    Cheers,

    JohnGG

Re: TK listbox escape text
by soonix (Canon) on Jun 26, 2015 at 11:38 UTC
    What is the "\x{9}"??
    That's a synonym for \t (documentation for this is buried in perlsyn perlop under "Quote and Quote-like Operators")

    For your original problem, maybe the simplest solution would be using sprintf (as already proposed), however in conjunction with a monospaced font (e.g. Courier).

Re: TK listbox escape text
by kcott (Archbishop) on Jun 26, 2015 at 20:35 UTC

    G'day jbuck,

    Welcome to the Monastery.

    Run the "Perl/Tk Widget Demonstration" (usually just referred to as the "WidgetDemo"):

    $ widget &

    Scroll down to the "Tix Widgets" section and click on:

    "7. Multicolumn listbox with individual cell styles"

    That seems to have the basic type of layout you're looking for. [It actually uses HList, not Listbox.]

    The [ See Code ] button will show how that was built; however, it was probably written more than a decade ago. The current documentation is: Tk::HList.

    See also Tk, which lists the current documentation for all the standard widgets.

    -- Ken

Re: TK listbox escape text
by Anonymous Monk on Jun 26, 2015 at 07:04 UTC
    Its simple, you use something that is not a Tk::Listbox, tabs are like newlines ... if you want them listbox is not for you
Re: TK listbox escape text
by soonix (Canon) on Jun 28, 2015 at 13:06 UTC
    When I wrote this, I had totally forgotten that I occasionally use Tk::GridColumns. This is a moduke of its own, not included in the main Tk module. Here is a simple example, which shows your environment variables example data:
    #!/usr/bin/env perl use strict; use warnings; use Tk; use Tk::GridColumns; my $mw = Tk::MainWindow->new(-title => 'ENV'); my $gc = $mw->GridColumns( # -data => [map {[$_, $ENV{$_}]} keys %ENV], -data => [['cherry', 'red fruit'], ['carrot', 'orange vegetable']] +, -columns => [{-text => 'Name'}, {-text => 'Value'} ], -colattr => {-anchor => 'w'}, -itemattr => {-anchor => 'w'}, )->pack( -fill => 'both', -expand => 1, ); $mw->MainLoop;
    BTW Tk::GridColumns can also become sortable, scrollable and editable, see EXAMPLES

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found