Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Tk::JComboBox -choices option

by Anonymous Monk
on Jun 11, 2017 at 14:05 UTC ( [id://1192530]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I can not figure out how I can dinamically set the -choices option of a Tk::JComboBox. This is a basic JComboBox code snippet.

use strict; use warnings; use Tk; use Tk::JComboBox; my $main = MainWindow->new (-title => "Test JComboBox"); my %TableHeaders=(1=>"English", 2=>"French", 3=>"German", 4=>"Undef", +5=>"Undef"); my $Language1; my $main ->JComboBox( -textvariable => \$Language1, -choices => [ {-name => $TableHeaders{1}, -value => 1}, {-name => $TableHeaders{2}, -value => 2}, {-name => $TableHeaders{3}, -value => 3}, {-name => $TableHeaders{4}, -value => 4}, {-name => $TableHeaders{5}, -value => 5}, ], )->pack();

In the previous code I want to be able to dynamically control the -choices so that, in my example, "Undef" values of %TableHeaders are not print (i.e. I will have only three elements in -choices). Any suggestions?

Replies are listed 'Best First'.
Re: Tk::JComboBox -choices option
by haukex (Archbishop) on Jun 11, 2017 at 14:54 UTC

    I'm not sure why you're using the string "Undef" instead of an actual undef, maybe that could be changed wherever you are generating %TableHeaders? Anyway, given what you've showed, here's a suggestion, this does an lc so the casing of "Undef" shouldn't matter:

    -choices => [ map { { -name=>$TableHeaders{$_}, -value=>$_ } } grep { length $TableHeaders{$_} && lc($TableHeaders{$_}) ne 'undef' } sort keys %TableHeaders ],

    If the keys of %TableHeaders are always numbers, you should do ... sort {$a<=>$b} keys ... instead.

      brilliant, thank you!

Re: Tk::JComboBox -choices option
by tybalt89 (Monsignor) on Jun 11, 2017 at 15:01 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1192530 use strict; use warnings; use Tk; use Tk::JComboBox; my $main = MainWindow->new (-title => "Test JComboBox"); $main->geometry('+800+500'); my %TableHeaders = (1=>"English", 2=>"French", 3=>"German", 4=>"Undef", 5=>"Undef"); my $Language1; my $jcb = $main ->JComboBox( -textvariable => \$Language1, )->pack(); sub setchoices { $jcb->configure( -choices => [ map +{ -name => $TableHeaders{$_}, -value => $_ }, grep $TableHeaders{$_} ne 'Undef', sort keys %TableHeaders ] ); } setchoices(); $main->Button( -text => "Add Spanish as 6", -command => sub { $TableHeaders{6} = 'Spanish'; setchoices(); }, )->pack(); $main->Button( -text => "Add Undef as 7", -command => sub { $TableHeaders{7} = 'Undef'; setchoices(); }, )->pack(); MainLoop;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found