Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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...

Rob

In reply to Re: TK::MListbox Issue by rcseege
in thread TK::MListbox Issue by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (11)
    As of 2014-09-02 17:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (25 votes), past polls