http://qs321.pair.com?node_id=1159186

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

Hello dear monks,
I am trying to use JBrowseEntry so I can have hidden tags for items in a listbox.
According to the specification of JBRowseEntry, if I insert a hashref then reading the text variable or using get should return the key corresponding to the value selected.
It doesn't seem to work this way.
Code snippet:
#!/usr/bin/perl -w use Tk; require Tk::JBrowseEntry; my %color_of = ( "apple" => "red", "orange" => "orange", "grape" => "purple", ); my $MW = MainWindow->new( -title => 'Jbrowse', -name => 'Demo', ); my $var = "red"; my $tmp_jbe = $MW->JBrowseEntry(-textvariable=>\$var, -width=>10, -state=>'normal', -btntakesfocus=>0, )->pack(); $tmp_jbe->insert('end',\%color_of); my $but = $MW->Button( -text => "Print Value", -width => 30, -height => 3, -command => sub { my $value = $tmp_jbe->get(); print "Var = $var, Get Result $value\n"; } )->pack(); MainLoop;
Thanks

Replies are listed 'Best First'.
Re: jbrowseentry using hashref does not seem to work
by flexvault (Monsignor) on Mar 31, 2016 at 10:15 UTC

    Dear Kafka,

    It appears to work on my Win10 box using Cygwin. Do you have X running?

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      You see the key when you press the button?
      i.e. not the value that appears in the listbox but the hidden key (in this case fruit type) that corresponds to the chosen color.
      I see just the color selected when I press the button.

        Kafka,

        I'm using your script on a monochrome Xterm, so I don't see the color change. When I click the button, I get:

        Var = purple, Get Result purple

        Are you looking for something else?

        Regards...Ed

        "Well done is better than well said." - Benjamin Franklin

Re: jbrowseentry using hashref does not seem to work
by flexvault (Monsignor) on Apr 04, 2016 at 17:31 UTC

    Kafka,

    I was hoping a monk with "Tk::JBrowseEntry" experience would give you some help, since I never worked with it, and only played with "Tk" a few times. That said, after you build your '%color_of' hash you could build another hash with the values of '%color_of' as the keys. ie: Untested!

    my %color_of = ( "apple" => "red", "orange" => "orange", "grape" => "purple", ); my %rev_color = (); foreach my $key ( keys %color_of ) { $rev_color{ $color_of{ $key } } = $key; }

    This allows you to lookup '$value' by using the contents of '$var' as the key for your '%rev_color' hash. ie:

    print "Var = $var, Get Result $rev_color{ $var }\n";

    Note: you can't have duplicate keys in the new hash.

    It may not be perfect, but it's a way of getting your desired results.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin