Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found