Some further commentary. A Tk::Text may not be your best choice to display and / or manipulate simple lists. It is possible to use them, but probably not optimal. You should likely at least make it a ROText to prevent unwanted modification. One of the List widgets seem like it would be better suited though.

Avoid cluttering up your script with globals. For small programs it isn't a big deal but as your script grows it will get harder keep track of them all. I like to use a single global hash to hold all of my Tk widget references. It keeps them tidy, makes it obvious what they are and generally makes for easier maintenance. This is just a personal preference but has been born through hard experience.

Narrow the scope of your subroutines. A sub called openFile() shouldn't do anything but return a file name (or file handle) you really shouldn't be doing any widget creation or text manipulation or any thing else in there. It will make debugging much easier down the road. Trust me...

You probably should install and use perltidy. It can really help make your code more readable.

If you want to be able to rearrange the order of your list of files you can do it elegantly with drag and drop. Here's a minimal example loosely based on your script using everything I mentioned above. (I was bored :-)

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::DragDrop; use Tk::DropSite; my %tk; $tk{lbfont} = '{Helvetica} 14'; my $trashcan = ' R0lGODlhFgAZAPcAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwMDcwKbK8EAgAGAgAIAgAK +AgAMAg AOAgAABAACBAAEBAAGBAAIBAAKBAAMBAAOBAAABgACBgAEBgAGBgAIBgAKBgAMBgAOBgAA +CAACCA AECAAGCAAICAAKCAAMCAAOCAAACgACCgAECgAGCgAICgAKCgAMCgAOCgAADAACDAAEDAAG +DAAIDA AKDAAMDAAODAAADgACDgAEDgAGDgAIDgAKDgAMDgAODgAAAAQCAAQEAAQGAAQIAAQKAAQM +AAQOAA QAAgQCAgQEAgQGAgQIAgQKAgQMAgQOAgQABAQCBAQEBAQGBAQIBAQKBAQMBAQOBAQABgQC +BgQEBg QGBgQIBgQKBgQMBgQOBgQACAQCCAQECAQGCAQICAQKCAQMCAQOCAQACgQCCgQECgQGCgQI +CgQKCg QMCgQOCgQADAQCDAQEDAQGDAQIDAQKDAQMDAQODAQADgQCDgQEDgQGDgQIDgQKDgQMDgQO +DgQAAA gCAAgEAAgGAAgIAAgKAAgMAAgOAAgAAggCAggEAggGAggIAggKAggMAggOAggABAgCBAgE +BAgGBA gIBAgKBAgMBAgOBAgABggCBggEBggGBggIBggKBggMBggOBggACAgCCAgECAgGCAgICAgK +CAgMCA gOCAgACggCCggECggGCggICggKCggMCggOCggADAgCDAgEDAgGDAgIDAgKDAgMDAgODAgA +DggCDg gEDggGDggIDggKDggMDggODggAAAwCAAwEAAwGAAwIAAwKAAwMAAwOAAwAAgwCAgwEAgwG +AgwIAg wKAgwMAgwOAgwABAwCBAwEBAwGBAwIBAwKBAwMBAwOBAwABgwCBgwEBgwGBgwIBgwKBgwM +BgwOBg wACAwCCAwECAwGCAwICAwKCAwMCAwOCAwACgwCCgwECgwGCgwICgwKCgwMCgwOCgwADAwC +DAwEDA wGDAwIDAwKDAwP/78KCgpICAgP8AAAD/AP//AAAA//8A/wD//////yH5BAEAAP8ALAAAAA +AWABkA BwjUAP8JHEiwoMGDCBMeBMAQgMKCDaVIJAUgicWEDe0h2CRlEykp9w7cY2gQwD179v4hOM +ByCwBS LO9RLJgE5gEEKFGSIiWT1BYpDgm+BIAg5L2jO5NKDCrUI8uiR49u2pKEqdCQmzzuzOpx6U +KY9liK TMqx6tcD9o4e4Hkvq9eSYHmu3Snlo1m4aOUqLWt14Mu8IfdKuQsxbmC6fM/a04t4cF+Bfx +cf/pgY r+S5Hz8CXRjy8l6qj/8BQPtPLE9SWUEjrBpyJdKfmxUCkDh4MOGHuHPjDggAOw== '; $tk{mw} = MainWindow->new; $tk{buttonframe} = $tk{mw}->Frame->pack( -pady => 3 ); $tk{load} = $tk{buttonframe}->Button( -text => 'Select (Multiple) Files', -command => sub { $tk{listbox}->insert( 'end', getFiles() ) }, )->grid( -row => 1, -column => 1 ); $tk{clear} = $tk{buttonframe}->Button( -text => 'Clear All', -command => sub { $tk{listbox}->delete( 0, 'end' ) }, )->grid( -row => 1, -column => 2 ); $tk{trash} = $tk{buttonframe}->Label( -image => $tk{mw}->Photo( -format => 'gif', -data => $trashcan, ), )->grid( -row => 1, -column => 3 ); $tk{process} = $tk{buttonframe}->Button( -text => 'Process Files', -command => sub { print join " + ", $tk{listbox}->get( 0, 'end' ); + }, )->grid( -row => 1, -column => 4 ); $tk{lbframe} = $tk{mw}->Frame->pack( -expand => 1, -fill => 'both', -anchor => 'nw' ); $tk{listbox} = $tk{lbframe}->Scrolled( 'Listbox', -selectmode => 'single', -scrollbars => 'ose', -font => $tk{lbfont}, )->pack( -expand => 1, -fill => 'both', -padx => 5, -pady => 5, ); my $dnd_token = $tk{listbox}->DragDrop( -event => '<B1-Motion>', -sitetypes => [qw/Local/], -startcommand => \&DragStart, ); $tk{listbox}->DropSite( -droptypes => [qw/Local/], -dropcommand => [ \&Drop, $tk{listbox}, $dnd_token ], ); MainLoop; sub DragStart { my ($token) = @_; my $site = $token->parent; my $e = $site->XEvent; my $idx = $site->index( '@' . $e->x . ',' . $e->y ); if ( defined $idx ) { $token->configure( -text => $site->get($idx), -font => $tk{lbf +ont} ); $site->delete($idx); my ( $X, $Y ) = ( $e->X, $e->Y ); $token->MoveToplevelWindow( $X, $Y ); $token->raise; $token->deiconify; $token->FindSite( $X, $Y, $e ); } } sub Drop { my ( $site, $token ) = @_; my $text = $token->cget('-text'); my $y = $site->pointery - $site->rooty; my $nearest = $site->nearest($y); if ( defined $nearest ) { my @xy = $site->bbox($nearest); if ( @xy and $xy[1] + $xy[3] > $y ) { $site->insert( $nearest, $text ); } else { $site->insert( 'end', $text ); } } } sub getFiles { my @types = ( [ "3d Files", '.3d' ], [ "Excel Files", '.xls' ], [ "All files", '*' ] ); my @files = $tk{mw}->getOpenFile( -filetypes => \@types, -defaultextension => '.3d', -title => 'Hold Down Ctrl Key To Select Multiple Fi +les', -multiple => 1, ); return @files; }

In reply to Re^3: TK Help by thundergnat
in thread TK Help by srikrishnan

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.