Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: TK::MListbox Issue

by rcseege (Pilgrim)
on Oct 06, 2005 at 03:03 UTC ( #497799=note: print w/replies, xml ) Need Help??

in reply to TK::MListbox Issue

I'm glad you were able to figure it out. Sometimes, when I'm trying to track down problems with a Tk script, I find that the more I can simplify it, the easier time I have finding the problem.

There's many approaches I take. Often, taking care in the way I format the code will be enough. Sometimes, when I find that I'm using the same options over and over again for sets of widgets, I'll put the options in an array or hash and assign the collection to the widget (or geometry manager). Sometimes things can be simplified through the use of creation functions/methods that hide the details of creating a particular widget -- especially when the widgets share options for creation, and geometry management.

Having said that, I took your script fragment, and rewrote it slightly to simplify it, as an example. It's a slightly over-the-top example, but I thought it might be helpful. I dropped out a few pieces that I thought might be non-essential.

By the way, you may want to double-check your select_tag and unselect_tag subs. You will lose entries if a user selects a non-contiguous group of entries, which is possible with the extended selectmode. Also, look how I used one sub to handle select/unselect and one for select/unselect all. Just a thought...

use strict; use warnings; use Tk; my $topLevel; my @tags = qw(one two three four five six); my $mw = MainWindow->new; createButton($mw, "Select Tags", \&selectTags); MainLoop; sub selectTags { if (!Exists($topLevel)) { $topLevel = $mw->Toplevel(-title=>"Report"); ## 1st Row - Listbox Pair my $f1 = createFrame($topLevel); my $lb = createListbox($f1); my $lb2 = createListbox($f1); $lb->insert("end", @tags); ## 2nd Row - Listbox Pair Controls my $f2 = createFrame($topLevel); createButton($f2, "> Select", [\&moveSelected, $lb, $lb2]); createButton($f2, "> Unselect", [\&moveSelected, $lb2, $lb]); createButton($f2, ">> Select All", [\&moveAll, $lb, $lb2]); createButton($f2, "<< Unselect All", [\&moveAll, $lb2, $lb]); ## 3rd Row - Do something with selection my $f3 = createFrame($topLevel); createButton($f3, "Do Something", [\&doSomething, $lb2]); } $topLevel->deiconify if !$topLevel->ismapped; $topLevel->raise; } sub doSomething { my $slb = shift; if ($slb->size > 0) { my $contents = join ",", $slb->get(0, 'end'); print "Contents: $contents\n"; $topLevel->withdraw; } else { print "Please make a selection first!\n"; } } sub moveAll { my ($lb, $lb2) = @_; $lb2->delete(0,'end'); $lb2->insert(0,@tags); $lb->delete(0,'end'); } sub moveSelected { my ($lb, $lb2) = @_; my @indices = $lb->curselection; if (@indices) { foreach my $i (@indices) { my $element = $lb->get($i); $lb2->insert('end', $element); } $lb->delete(pop @indices) while @indices; $lb->selectionClear(0,'end'); } else { print "Listbox selection error!\n"; print "Please make a selection first\n\n"; } } ## Simple widget creation routines, intended to illustrate ## one way of reducing duplication. sub createButton { my ($parent, $label, $sub) = @_; return $parent->Button( -command => $sub, -text => $label, )->pack(qw/-expand 1 -side left -fill x/); } sub createFrame { return shift->Frame( -borderwidth => 2, -relief => 'groove' )->pack(qw/-side top -fill x/); } sub createListbox { return shift->Scrolled("Listbox", -scrollbars => 'osoe', -selectmode => 'extended' )->pack(qw/-side left -fill y/); }

Anyhow, hope this is useful...


Replies are listed 'Best First'.
Re^2: TK::MListbox Issue
by Anonymous Monk on Oct 06, 2005 at 12:25 UTC
    Thanks for the follow-up, Rob. Your modification are certainly a lot better and more succint. I'll study them and learn from them. Much appreciated.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://497799]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2020-10-25 02:56 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (248 votes). Check out past polls.