http://qs1969.pair.com?node_id=11139147

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

I have trouble with focus and a HList.

I wanted my HList to have clickable headers:

The code snippet attached at the end is the simplest test-case I could come up with.

You might think "Why would anyone have a GUI like this" ... but this code is purely to demonstrate the problem.

With the script running, and with the window made nice'n'wide, to demonstrate the problem:

Isn't it curious that the highlight rectangle doesn't appear when in the HList entries when the mouse transitions through the Label and button widgets?

You may well be asking, "why is this important" ... well because, in my full script (not in this example) I have a keyboard binding for the Escape-key to unselect all HList entries ... but this only works when the HList has the keyboard focus ... and the keyboard focus seems to be very dependent on the route the mouse took to get to the HList.

Questions:

Thank you very much for your time.

Andrew

#!/usr/bin/perl use Tk; use Tk::HList; my $mw = MainWindow->new(); $b1 = $mw->Button(-text=>"Button1")->pack(); $b2 = $mw->Button(-text=>"Button2")->pack(); $hlist = $mw->HList( -selectmode => 'single', -columns=>3, -background +=>"Wheat", -header=>1)->pack(-fill=>'both',-expand=>1); $hlist -> focusFollowsMouse(); $hlist -> headerCreate(0, -text=>"ABC"); $label = $hlist->Label(-text=>"DEF", -pady=>1); $hlist -> headerCreate(1, -itemtype=>"window", -window=>$label); $button = $hlist->Button(-text=>"GHI"); $hlist -> headerCreate(2, -itemtype=>"window", -window=>$button); for (1..9) { $hlist->add($_); $hlist->itemCreate($_, 0, -text=>"$_"x5); $hlist->itemCreate($_, 1, -text=>"$_"x10); } MainLoop;

Replies are listed 'Best First'.
Re: TK/HList and focus
by choroba (Archbishop) on Nov 26, 2021 at 17:18 UTC
    That's because the label or the button steal the focus from the HList. You can prevent it with
    $_->bind('<FocusIn>', sub { $hlist->focus }) for $label, $button;

    but it might interfere with how sorting the columns works which you haven't showed.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      That's brilliant. Thank you very much. Works a treat.

      Andrew