I have an entry widget which has a binding for "<Key-Return>" and I have
a listbox widget which has a bidning for "<<ListboxSelect>>".
Both bindings work OK: if I enter something into the entry widget, it
runs the handler, if I select a listbox entry uwing the mouse, it fires
the handler. However, when I call "selectionSet()" from the entry
widget's "<Key-Return>" handler with the index of an entry in the
listbox, the "<<ListboxSelect>>", the "<<ListboxSelect>>" virtual event
is not generated.
From the man page, I inferred that it should have been: "Any time the
selection changes in the listbox, the virtual event <<ListboxSelect>>
will be generated."
The following shows what I mean:
If you click on an entry in the list, the text appears at the bottom.
If you enter an entry's text, the entry will be selected, but the label
will not be modified.
What am I doing wrong?
Josef
#! /usr/bin/perl
use warnings;
use strict;
use Tk;
my $top = new MainWindow;
my $ent = $top->Entry(-width => 10)->pack(-side => 'top');
my $list = $top->Listbox(-height => 5, -selectmode =>
'browse')->pack(-side => 'top');
my $lab = $top->Label(-text => 'XXX')->pack(-side => 'top');
my $listlen = $list->size;
my $pat = $ent->get();
for (my $n = 0; $n < $listlen; $n++) {
my $item = $list->get($n);
if ($item =~ /$pat/) {
$list->selectionSet($n);
last;
}
}
}
sub show {
my ($self, $list, $lab) = @_;
my $idx = $list->curselection;
my $text = $list->get($idx);
$lab->configure(-text => $text);
}
-- These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
> I have an entry widget which has a binding for "<Key-Return>" and I have
> a listbox widget which has a bidning for "<<ListboxSelect>>".
> Both bindings work OK: if I enter something into the entry widget, it
> runs the handler, if I select a listbox entry uwing the mouse, it fires
> the handler. However, when I call "selectionSet()" from the entry
> widget's "<Key-Return>" handler with the index of an entry in the
> listbox, the "<<ListboxSelect>>", the"<<ListboxSelect>>" virtual event
> is not generated.
> From the man page, I inferred that it should have been: "Any time the
> selection changes in the listbox, the virtual event<<ListboxSelect>>
> will be generated."
> The following shows what I mean:
> If you click on an entry in the list, the text appears at the bottom.
> If you enter an entry's text, the entry will be selected, but the label
> will not be modified.
> What am I doing wrong?
I'm guessing since you added the return handler, you have to link the
event to the handler yourself (the existing mouse/kybd handlers are
already linked to generate the event). Note the change to your search
routine(I cleared the selection [just for looks] and left your set, but
followed it with an eventGenerate):
use strict;
use warnings;
use Tk;
my $top = new MainWindow;
my $ent = $top->Entry(-width => 10)->pack(-side => 'top');
my $list = $top->Listbox(-height => 5, -selectmode => 'browse')->pack(
-side => 'top');
my $lab = $top->Label(-text => 'XXX')->pack(-side => 'top');
> [...]
> From the man page, I inferred that it should have been: "Any time the
> selection changes in the listbox, the virtual event<<ListboxSelect>>
> will be generated."
IMHO this means 'by the user', not programmatically.
> What am I doing wrong?
Well, you already know that the selection has changed after your
$list->selectionSet($n);
so trigger that event by yourself:
$list->eventGenerate('<<ListboxSelect>>');
But note one more thing: you will get errors after the first entry,
since your code generates *multiple selections* this way.
You probably want an addtl.
$list->selectionClear(0,'end');
before setting the new one.
HTH, Horst
-- <remove S P A M 2x from my email address to get the real one>
> Josef Moellers schrieb am 03.04.2012 15:27:
>> [...]
>> From the man page, I inferred that it should have been: "Any time the
>> selection changes in the listbox, the virtual event<<ListboxSelect>>
>> will be generated."
> IMHO this means 'by the user', not programmatically.
>> What am I doing wrong?
> Well, you already know that the selection has changed after your
> $list->selectionSet($n);
> so trigger that event by yourself:
> $list->eventGenerate('<<ListboxSelect>>');
> But note one more thing: you will get errors after the first entry,
> since your code generates *multiple selections* this way.
> You probably want an addtl.
> $list->selectionClear(0,'end');
> before setting the new one.
Thanks, also to $Bill.
Strangely, this works in the simple example I provided, but in the
slightly larger one that I am writing (involving database accesses
etc.), I have to pass $list->Subwidget('scrolled') when binding to
<Key-Return> to the entry widget. Passing $list did not work: the
handler was not invoked.
Thanks again to both of you,
Josef
-- These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
> Strangely, this works in the simple example I provided, but in the
> slightly larger one that I am writing (involving database accesses
> etc.), I have to pass $list->Subwidget('scrolled') when binding to
> <Key-Return> to the entry widget. Passing $list did not work: the
> handler was not invoked.
It's probably the scoping of $list. Try using 'our $list = ... ;' and make
sure it's declared prior to the routine you pass to. I had a similar
scoping problem that I actually wrote an embedded sub in the GUI
definition sub and had it return a ref to the obj/widget.
>> Strangely, this works in the simple example I provided, but in the
>> slightly larger one that I am writing (involving database accesses
>> etc.), I have to pass $list->Subwidget('scrolled') when binding to
>> <Key-Return> to the entry widget. Passing $list did not work: the
>> handler was not invoked.
> It's probably the scoping of $list. Try using 'our $list = ... ;' and make
> sure it's declared prior to the routine you pass to. I had a similar
> scoping problem that I actually wrote an embedded sub in the GUI
> definition sub and had it return a ref to the obj/widget.
No show :-(
But then ... if it works with the Subwidget call, I'm happy.
Thanks again,
Josef
-- These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html