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

Hello Monks,

I’m new to the monetary so please forgive any unintentional broken rules.
I am working on a graphical interface that will be used to speed up data
retrieval from a remote server @ my workplace. My problem is not life or
death I’ve already got it working with a separate button, but I’d like to
be able to bind the delete button to delete unwanted entries from a scrolled
listbox, which I have working the only problem is I can’t get the right
widget to take focus on a button click. The current code makes the listbox
take focus but the button bind won’t work. However, if I tab from the entry
widget to the frame that contains the scolled widget the button bind works
flawlessly. Is there some way I can bind to <ButtonRelease-1> to focus on
the Tk::Frame that contains the widget?

System:

OS: HP-UX 10.2 and WinXP
PERL: build 5.0004 on HP and 5.8.8 on XP

# --------------------------------------
# ----- Parameter List and Delete Button
# --------------------------------------
$p2->Label( -width=>$testlbl,
-text=>"Parameter List",
-foreground=>'#FFFFFF',
-background=>'#666666',
-font=>$font,
-anchor=>'center')->
grid(-row => 6, -column => 0, -columnspan=>5, -padx=>5, -pady=>0, -sticky => 'ew');

$lstbox = $p2->Scrolled( "Listbox",
-scrollbars=>'osoe',
-selectmode=>'extended',
-foreground=>'#FFFFFF',
-background=>'#7E7E68',
-font=>$font,
-width=>40 )->
grid(-row => 7, -column =>0, -columnspan=>5, -padx=>5, -pady=>0, -sticky => 'nsew');

$lstbox->Subwidget("corner")->configure( -background=>'#666666' );
$lstbox->Subwidget("xscrollbar")->configure(-background=>'#666666' );
$lstbox->Subwidget("yscrollbar")->configure(-background=>'#666666' );
$lstbox->bind( '<ButtonRelease-1>' =>\sub{ $lstbox->focus});
$lstbox->bind($lstbox, '<Delete>' =>\&Deltxt );

Replies are listed 'Best First'.
Re: Problem with Correct Widget focus();
by thundergnat (Deacon) on May 24, 2007 at 18:17 UTC

    You typically only need to put the tag in the binding if it refers to a window. Basically, just removing the $lstbox tag from the bind to <Delete> command makes it work.

    BTW, it is usually better if you can provide a complete runable script when you ask a question. A lot of times, if there isn't a framework to run your code, your question will get fewer responses.

    I built a small demo script using your posted code. Try this.

    Update: Whoops, didn't notice extended select mode. Changed Deltxt sub appropriately.

    use strict; use warnings; use Tk; my $testlbl = 15; my $font = '{New Times Roman} 12'; my $p2 = MainWindow->new; $p2->Label( -width => $testlbl, -text => "Parameter List", -foreground => '#FFFFFF', -background => '#666666', -font => $font, -anchor => 'center' )->grid( -row => 6, -column => 0, -columnspan => 5, -padx => 5, -pady => 0, -sticky => 'ew' ); my $lstbox = $p2->Scrolled( "Listbox", -scrollbars => 'osoe', -selectmode => 'extended', -foreground => '#FFFFFF', -background => '#7E7E68', -font => $font, -width => 40 )->grid( -row => 7, -column => 0, -columnspan => 5, -padx => 5, -pady => 0, -sticky => 'nsew' ); $lstbox->Subwidget("corner")->configure( -background => '#666666' +); $lstbox->Subwidget("xscrollbar")->configure( -background => '#666666' +); $lstbox->Subwidget("yscrollbar")->configure( -background => '#666666' +); $lstbox->bind( '<ButtonRelease-1>' => sub { $lstbox->focus } ); $lstbox->bind( '<Delete>' => \&Deltxt ); $lstbox->insert( 'end', $_ ) for ( 1 .. 20 ); MainLoop; sub Deltxt { return unless my @indicies = $lstbox->curselection; $lstbox->delete( $_ ) for @indicies; }
      Thank you thundergnat

      I had tried that exact thing before I figured out that I needed to have focus
      on the widget to get it to work but somehow migrated away from it thank you for
      on the right track. Your code fixed it in Win32 but I had to add focus specifically
      to the subwidget listbox to get it to work on HP-UX. Again thank you. As crazy
      as it sounds I've been beatin my head on this one in my free time for a week.

      $lstbox->bind( '<ButtonRelease-1>' =>\sub{ $lstbox->Subwidget("listbox +")->focus() }); $lstbox->bind( '<Delete>' => \&Deltxt );