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

Hello Monks,

I have an issue with mouse wheel scrolling when I have frame created with the Scrolled method, and that frame contains listboxes. I can scroll the toplevel frame just fine with the mouse wheel when it is the focus and mouse wheel scrolling has been enabled.

However, when I click on a listbox (making it active) and scroll through it with the mouse wheel, the toplevel frame also scrolls. How can I disable the mouse wheel scrolling in the toplevel frame when the focus is in a listbox? Here is a simple example that shows the issue and thank you in advance:

use Tk; use Tk::BrowseEntry; use Tk::Pane; use Term::ReadKey; use strict; my ($mw,$topframe,$font,$row,$i,$who); my (@frame,@list,@label); my (%var); $mw = MainWindow->new(); $mw->title("Test GUI"); $mw->bind('<KeyPress-Return>', sub{shift->focusNext}); $mw->geometry('1000x300+400+0'); $topframe = $mw->Scrolled('Frame', -scrollbars => 'e',-height => 1000) +->pack(); $mw->bind('all','<4>',=>sub{$topframe->yview('scroll',3,'units')}); $mw->bind('all','<5>',=>sub{$topframe->yview('scroll',3,'units')}); $font = "-adobe-times-bold-r-normal--20-100-75-75-p-p-56-iso8859-1"; $row = 1; for ($i =0; $i < 100; $i++) { $frame[$i] = $topframe->Frame(-borderwidth => '1', -relief => +'groove'); $list[$i] = $frame[$i]->BrowseEntry(-variable => \$var{$i}, -takefocus => 0, -foreground => 'black', -disabledforeground => 'black', -highlightcolor => 'green', -font => $font, -width => 50, -state => 'readonly') ->pack(-side => "left", +-padx => "5"); $label[$i] = $frame[$i]->Label(-text => $i, -anchor => 'w', -takefocus => 0, -foreground => 'black', -font => $font, -width => 50, -height => 1) ->pack(-side => "right", -padx +=> 5); $frame[$i]->grid(-row => ++$row, -column => 1, -sticky => 'e', + -padx => 5); $list[$i]->insert('end', (1..100)); } MainLoop();

Replies are listed 'Best First'.
Re: Perl Tk issue with mouse wheel scrolling
by zentara (Cardinal) on Aug 02, 2014 at 09:59 UTC
    This works, just remove 'all' from your bind, as suggested above. Also, Scrolled Frames are not recommended, use a Scrolled Pane.
    #!/usr/bin/perl use Tk; use Tk::BrowseEntry; use Tk::Pane; use strict; my ($mw,$topframe,$font,$row,$i,$who); my (@frame,@list,@label); my (%var); $mw = MainWindow->new(); $mw->geometry('1000x300+400+40'); $mw->title("Test GUI"); $mw->bind('<KeyPress-Return>', sub{shift->focusNext}); $topframe = $mw->Scrolled('Pane', -scrollbars => 'e',-height => 1000)- +>pack(); #$mw->bind('all','<4>',=>sub{$topframe->yview('scroll',3,'units')}); #$mw->bind('all','<5>',=>sub{$topframe->yview('scroll',3,'units')}); $mw->bind('<4>',=>sub{$topframe->yview('scroll',3,'units')}); $mw->bind('<5>',=>sub{$topframe->yview('scroll',3,'units')}); $font = "-adobe-times-bold-r-normal--20-100-75-75-p-p-56-iso8859-1"; $row = 1; for ($i =0; $i < 100; $i++) { $frame[$i] = $topframe->Frame(-borderwidth => '1', -relief => +'groove'); $list[$i] = $frame[$i]->BrowseEntry(-variable => \$var{$i}, -takefocus => 0, -foreground => 'black', -disabledforeground => 'black', -highlightcolor => 'green', -font => $font, -width => 50, -state => 'readonly') ->pack(-side => "left", +-padx => "5"); $label[$i] = $frame[$i]->Label(-text => $i, -anchor => 'w', -takefocus => 0, -foreground => 'black', -font => $font, -width => 50, -height => 1) ->pack(-side => "right", -padx +=> 5); $frame[$i]->grid(-row => ++$row, -column => 1, -sticky => 'e', + -padx => 5); $list[$i]->insert('end', (1..100)); } MainLoop();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you all very much for the help!!!!
Re: Perl Tk issue with mouse wheel scrolling (Tk::DynaMouseWheelBind)
by Anonymous Monk on Aug 02, 2014 at 08:19 UTC

    and scroll through it with the mouse wheel, the toplevel frame also scrolls. How can I disable the mouse wheel scrolling in the toplevel frame when the focus is in a listbox?

    Well :) don't bind to all, bind to something else, like $frame

    Or filter the events you get, ignore based on the tk variables $Tk::event and $Tk::widget ... for example, don't scroll if $Tk::widget is not $topframe

    Or try Tk::DynaMouseWheelBind ...