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

I've been converting to TKx from Tk, and i've got a problem with TCL formatted lists going into an HList widget.

I found this to convert an array into a TCL list and it's always worked for me:

$cnames = ''; foreach $i (@countrynames) { $cnames = $cnames . ' {' . $i . '}'; };

However, if you pass in strings that have braces in them, it confuses the TKx widget... (Generally the Hlist will be empty)

So I tried escaping the braces in the array. That works, but now I see the escapes showing in my GUI. Is there a way around this? Thanks

Replies are listed 'Best First'.
Re: TCL list via TKx
by Anonymous Monk on Jul 07, 2012 at 20:50 UTC

    I think you found a really stupid bug :)

    You could try escaping the string as per the trace from http://www.tkdocs.com/tutorial/onepage.html

    #!/usr/bin/perl -- use Tkx; $Tkx::TRACE = 64; Tkx::package_require('tile'); my $mw = Tkx::widget->new("."); # example bug : new_tk__listbox ( $lb = $mw->new_listbox( -height => 5 ) ) ->g_grid( -column => 0, -row => 0, -sticky => "nwes" ); ( $s = $mw->new_ttk__scrollbar( -command => [ $lb, "yview" ], -orient => "vertical" ) )->g_grid( -column => 1, -row => 0, -sticky => "ns" ); $lb->configure( -yscrollcommand => [ $s, "set" ] ); ( $mw->new_ttk__label( -text => "Status message here", -anchor => "w" ) )->g_grid( -column => 0, -row => 1, -sticky => "we" ); ( $mw->new_ttk__sizegrip )->g_grid( -column => 1, -row => 1, -sticky = +> "se" ); $mw->g_grid_columnconfigure( 0, -weight => 1 ); $mw->g_grid_rowconfigure( 0, -weight => 1 ); for ( $i = 0 ; $i < 100 ; $i++ ) { #~ $lb->insert( "end", "Line " . $i . " of 100" ); $lb->insert( "end", "Line " . $i . " of {100}" ); } Tkx::MainLoop();

    Watch the trace

    ... Tkx-114-0.1s-tkx.listbox.pl-30: .l insert end {Line 97 of \{100\}} Tkx-115-0.1s-tkx.listbox.pl-30: .l insert end {Line 98 of \{100\}}

      I don't completely follow what you're saying. Just that using ->insert will work with escaped braces, and that escaping in the list should work the same? In playing around, what's really weird is if I set something up with braces using the list at initialization, tkx will tell me about the unmatched brace and die. But if it happens during runtime, it just falls thorugh...

      It's funny because that's how I had my original Tk using hlist with $hlist->add calls. I didn't think Tkx had a similiar interface...

      Thanks. I'm going to use the ->insert.

      # here's what I mean, I hacked your example # the trip button adds a third element, which gets dropped # silently. (and you can escape it, and see the escape... use Tkx; $Tkx::TRACE = 64; Tkx::package_require('tile'); my $mw = Tkx::widget->new("."); my $list; my @strings; $strings[0] = 'Hello, this should be ok'; $strings[1] = 'this too'; $list = &make_tcl_list(@strings); # example bug : new_tk__listbox ( $lb = $mw->new_listbox( -height => 5,-listvariable => \$list ) ) ->g_grid( -column => 0, -row => 0, -sticky => "nwes" ); ( $s = $mw->new_ttk__scrollbar( -command => [ $lb, "yview" ], -orient => "vertical" ) )->g_grid( -column => 1, -row => 0, -sticky => "ns" ); $lb->configure( -yscrollcommand => [ $s, "set" ] ); ( $mw->new_ttk__label( -text => "Status message here", -anchor => "w" ) )->g_grid( -column => 0, -row => 1, -sticky => "we" ); ( $mw->new_ttk__sizegrip )->g_grid( -column => 1, -row => 1, -sticky = +> "se" ); $mw->g_grid_columnconfigure( 0, -weight => 1 ); $mw->g_grid_rowconfigure( 0, -weight => 1 ); ## add a button to add an unbalanced brace (my $button = $mw->new_tk__button( -text=>"Trip", -anchor=>'w', -command=> sub { $strings[2] = 'watch this{'; $list=&make_tcl_list(@strings); } ))->g_grid( -column => 0, -row => 1, -sticky => "nwes" ); Tkx::MainLoop(); sub make_tcl_list{ my @LIST = @_; my $listx = ''; for my $i (@LIST) { $listx = $listx . ' {' . $i . '}'; } return $listx; }