in reply to Re^2: TK Help
in thread TK Help
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: TK Help
by srikrishnan (Beadle) on May 06, 2010 at 07:01 UTC | |
by afoken (Chancellor) on May 06, 2010 at 07:45 UTC | |
by srikrishnan (Beadle) on May 07, 2010 at 04:01 UTC | |
|
Re^4: TK Help
by srikrishnan (Beadle) on May 08, 2010 at 04:24 UTC |