Hi Perl Monks, I'm trying to create a GUI which consists of several BrowseEntries and stores the Index and the Value of every Selection for further use. I'm doing this via the -browsecmd Option. But since there will be a lot of BrowseEntries and I don't want to write as many Subroutines I want to store the BrowseEntries in a Hash and just pass their key to a single Subroutine like in the code below:

#!usr/bin/perl use strict; use warnings; use Tk; use Tk::BrowseEntry; my $mw = MainWindow->new(); my %entries; my %entries_list; #Definition of Browseentry 1 ############### $entries{option1} = $mw->BrowseEntry( -browsecmd=>\&selection("option1") )->pack(); $entries_list{option1} = $entries{option1}->Subwidget("slistbox"); for (qw/Word1 Word2 Word3 Word4/) { $entries{option1}->insert("end", $_); } #Definition of Browseentry 2 ############### $entries{option2} = $mw->BrowseEntry( -browsecmd=>\&selection("option2") )->pack(); $entries_list{option2} = $entries{option2}->Subwidget("slistbox"); for (qw/Word5 Word6 Word7 Word8/) { $entries{option2}->insert("end", $_); } ########################################### $mw->MainLoop(); exit 0; ########################################### sub selection { print "Index: ".$entries_list{$_[0]}->curselection ." Value: ".$entries_list{$_[0]}->get( $entries_list{$_[0]}->curselection() )."\n"; #<-this is where the errormessage points } #selection

The Problem is, that -browsecmd calls the selection Subroutine upon loading the GUI when i pass a Parameter and i get:
"Can't call method "curselection" on an undefined value at code_snippet.pl line 29."
Makes sense since there is no Subwidget defined when selection is called. It does work though if i don't pass a Parameter, but then i can't use a single Subroutine for every BrowseEntry.

I tried adding  return if ! defined $entries_list{$_[0]}; to the Subroutine but that leads to the following errormessage
Tk::Error: Odd number of args to Tk::BrowseEntry->new(...)
Tk callback for .
Tk::Widget::new at /usr/lib64/perl5/vendor_perl/Tk/Widget.pm line 164
Tk::Widget::__ANON__ at /usr/lib64/perl5/vendor_perl/Tk/Widget.pm line 256
Odd number of args to Tk::BrowseEntry->new(...)
at code_snippet.pl line 15

Any advice is highly welcomed. ;)


In reply to Tk::Browseentry -browsecmd Option with Subroutine Parameter by erpse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.