(after MainLoop has been called)
Not after the MainLoop. This loop never exits (if you don't destroy the MainWindow, that is).
Instead, you bind events to code (subroutines), which does what you want (e.g. create widgets). Then if a event is triggered, the code bound to this event is executed.
populate a grid managed frame
You just create those widgets as children of the frame:
$Frame->$Widget(@args);
where $Widget is Button, Frame, Entry, Canvas, ... etc. | [reply] [d/l] |
| [reply] |
Try HList, just put the code below to open in a new toplevel, or frame. Also check out a wacky widget I made at Tk-CanvasDirTree, which is a dynamic tree on a canvas. In the below example, you would put the
HLists into a Scrolled::Pane, but this simple example just dumps them adjacent to the last in a simple Frame.
#!/usr/bin/perl
use strict;
use Tk;
use Tk::HList;
my $mw = MainWindow->new();
$mw->geometry("500x500+100+100");
my $h;
#create some sample data
my %data;
foreach (0..100) {
$data{$_}{'name'} = 'name'.$_;
$data{$_}{'id'} = rand(time);
}
#get random list of keys
my @keys = keys %data;
#################
my $button0 = $mw->Button(-text => 'show list',
-command => sub{ showlist() } )->pack;
my $button = $mw->Button(-text => 'exit',
-command => sub{exit})->pack;
my $sortid = $mw->Button(-text => 'Sort by Id',
-command => [\&sort_me,1] )->pack;
MainLoop;
#########################################################
sub showlist{
$h = $mw->Scrolled( 'HList',
-header => 1,
-columns => 2,
-width => 30,
-height => 60,
-takefocus => 1,
-background => 'steelblue',
-foreground =>'snow',
-selectmode => 'single',
-selectforeground => 'pink',
-selectbackground => 'black',
# -browsecmd => \&browseThis,
)->pack(-side => "left", -anchor => "n");
my $nameh = $h->header('create', 0, -text => ' Name ',
-borderwidth => 3,
-headerbackground => 'steelblue',
-relief => 'raised');
my $idh = $h->header('create', 1, -text => ' ID ',
-borderwidth => 3,
-headerbackground => 'lightsteelblue',
-relief => 'raised');
foreach (@keys) {
my $e = $h->addchild(""); #will add at end
$h->itemCreate ($e, 0,
-itemtype => 'text',
-text => $data{$_}{'name'},
);
$h->itemCreate($e, 1,
-itemtype => 'text',
-text => $data{$_}{'id'},
);
}
}
sub sort_me{
my $col = shift;
my @entries = $h->info('children');
my @to_be_sorted =();
foreach my $entry(@entries){
push @to_be_sorted,
[ $h->itemCget($entry,0,'text'),
$h->itemCget($entry,1,'text')
];
}
my @sorted = sort{ $a->[$col] cmp $b->[$col] } @to_be_sorted;
my $entry = 0;
foreach my $aref (@sorted){
# print $aref->[0],' ',$aref->[1],"\n";
$h->itemConfigure( $entry, 0, 'text' => $aref->[0] );
$h->itemConfigure( $entry, 1, 'text' => $aref->[1] );
$entry++;
}
$mw->update;
}
| [reply] [d/l] |
I looked at HList but it wasn't clear that I could easily create a scrolling grid of thumbnails with it. I want a rectangular area with rows and columns of thumbnails that I can select and operate on. (Ultimately I'd like to be able to remove thumbnails as operations are completed but that's for later.)
| [reply] |
You can use Hlist for thumbnails, see ztkdb and ztkdb-sql
HList is the most configurable list in Tk.
You can also use Gtk2's various list widgets, which accept pixbuf fields.
| [reply] |
Can you post the appropriate code so we can get a bigger picture of what you're trying to do? | [reply] |
In my callback I have a call to this subroutine:
sub ThumbNail {
my($name) = @_;
my $row = $img_id % 4;
my $col = $img_id - 4 * $row;
my $img = $frame->Photo(-format => 'jpeg', -file => $name);
my $iratio = $img->width()/200;
my $nh = $img->height()/$iratio;
my $newimg = $frame->Photo(-width => 200, -height => $nh);
$newimg->copy($img, -subsample=>($iratio, $iratio));
my $imgL = $frame->Label('-image' => $newimg)->pack;
$imgL->bind('<Button-1>', [\&doFocus, $imgL] );
$imgL->bind ( '<Key-1>' , [\&rate, $img_id, 1]);
$imgL->bind ( '<Key-2>' , [\&rate, $img_id, 2]);
$imgL->bind ( '<Key-3>' , [\&rate, $img_id, 3]);
$imgL->bind ( '<Key-4>' , [\&rate, $img_id, 4]);
$imgL->bind ( '<Key-5>' , [\&rate, $img_id, 5]);
@imgs[$img_id] = { name => $name, lbl => $imgL, sel => 0 };
$img_id++;
return $imgL;
}
Some command windows flicker and then it aborts. | [reply] [d/l] |
Using the command window and -W I found that there were errors messing up the code. This wasn't a widget display problem after all. I still get flashing command windows for every image created... | [reply] |