Skip to Main Content
Learning Perl/Tk
book

Learning Perl/Tk

by Nancy Walsh
January 1999
Beginner content levelBeginner
373 pages
9h 43m
English
O'Reilly Media, Inc.
Content preview from Learning Perl/Tk

7.18. Listbox Example

Sometimes when you put a lot of items in a listbox, it takes a long time to scroll through the listbox. If you insert the items in the listbox sorted, you can implement a search routine. Here's a quick script that shows you how to use an entry widget to input the search text and then search the listbox every time you get a new character in the entry:

use Tk; $mw = MainWindow->new; $mw->title("Listbox"); # For example purposes, we'll use one word for each letter @choices = qw/alpha beta charlie delta echo foxtrot golf hotel india juliet kilo lima motel nancy oscar papa quebec radio sierra tango uniform victor whiskey xray yankee zulu/; # Create the entry widget, and bind the do_search sub to any keypress $entry = $mw->Entry(-textvariable => \$search)->pack(-side => "top", -fill => "x"); $entry->bind("<KeyPress>", [ \&do_search, Ev("K") ]); # Create listbox and insert the list of choices into it my $lb = $mw->Scrolled("Listbox", -scrollbars => "osoe", )->pack(-side => "left"); $lb->insert("end", sort @choices); $mw->Button(-text => "Exit", -command => sub { exit; })->pack(-side => "bottom"); MainLoop; # This routine is called each time we push a keyboard key. sub do_search { my ($entry, $key) = @_; # Ignore the backspace key and anything that doesn't change the word # i.e. The Control or Alt keys return if ($key =~ /backspace/i); return if ($oldsearch eq $search); # Use what's currently displayed in listbox to search through # This is a non-complicated in order ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Perl/Tk

Mastering Perl/Tk

Stephen Lidie, Nancy Walsh

Publisher Resources

ISBN: 1565923146Supplemental ContentCatalog PageErrata