zzspectrez has asked for the wisdom of the Perl Monks concerning the following question:
Hello All....
It has been quite a while since I have played with perl, or visited the monestary. I just recently started playing with perl and perl/tk to display some thumbnails of images which I would like the user to be able to select by clicking on the images.
I was able to setup a callback to the button click or image click. So selection will not be an issue. However, I want it to be visible which images are selected. Since the call back is passed a ref to the widget, I thought I could either select a thick border or change the relief option to make it visible. These options when changed do not appear visualy. I suppose that the -image option must override these options. Any suggestions other than rewriting Tk::Thumbnail to add checkboxes??
Here is the code I have so far.
use Tk;
use Tk::Thumbnail;
## GLOBALS
my $mw;
my $thumb;
## Subroutines
sub thumb_cmd {
my ( $widget, $filename, $is_bad,
$pix_width, $pix_height, $frames ) = @_;
if ($is_bad) {
print "IS INVALID IMAGE\n";
return;
}else{
print "Valid Image.\n";
print "Thumbnail information:\n";
print "\tFilename: [$filename]\n";
print "\tWidth: $pix_width\n";
print "\tHeight: $pix_height\n";
## I Tried adding code that modified
## options of $widget...
## ie... $widget->configure( -relief => 'raised' );
return;
}
}
sub init {
my @img = ( <*.jpg>, <*.bmp> );
$mw = MainWindow->new;
$thumb = $mw->Thumbnail(
-images => \@img,
-ilabels => 1,
-iheight => 75,
-iwidth => 75,
-command => \&thumb_cmd,
)->pack;
}
## MAIN
init;
MainLoop;
zzSPECTREz
Re: Tk::Thumbnail --> callback help
by zentara (Archbishop) on Apr 14, 2005 at 21:35 UTC
|
Here, I chopped down the script I mentioned above, to just be a thumbnail displayer, with HList. The thumbnails are stored in memory, not disk files. I've left the HList -selectmode => 'single', and you can work out how you would
like to display multiple files.If you use -selectmode=>'multiple', you can use the "shift-left-mouse-click" to select multiple thumbnails, but then you will need to work out a display strategy. Maybe a "right-mouse-click" binding on the HList, along with multiple labels(with images) in the main frame. You can also put checkbuttons in the HList if you want. Anyways, this should get you started.
I'm not really a human, but I play one on earth.
flash japh
| [reply] [Watch: Dir/Any] [d/l] |
|
> Here, I chopped down the script I mentioned above, to just be a thumbnail displayer....
Thanks for your efforts! :p
The Imager module looks real good.. Unfortunatley, I am having a challenge getting it working with windows Activestate. There is no ppm available, and my first attempts with CPAN have been unsucessfull.
This weekend I will be trying again to get this module to compile and install on my windows box.
zzSPECTREz | [reply] [Watch: Dir/Any] |
|
Well, if you can't get Imager installed, ImageMagick should be able to do the same thing. You might have to fiddle a bit with the syntax for writing the thumbnail to data memory, but it should be possible. Or ask where you can get a ppm for Imager.
I'm not really a human, but I play one on earth.
flash japh
| [reply] [Watch: Dir/Any] |
Re: Tk::Thumbnail --> callback help
by zzspectrez (Hermit) on Apr 14, 2005 at 04:39 UTC
|
I guess TK isn't that popular!
I hate to be the first to respond to my own thread, but here goes.
I stole borrowed some code from the Tk::Thumbnail module and came up with an immature solution. However, 1) I hate reinventing the wheel 2) My hack of the original code is not as elegant.
Seems like there should be an easier way to do this that utilizes the existing Tk::Thumbnail.. My thought is that I need to subclass this module.. Tk::Thumbnail::Select but am unsure how I would do this.. The whole TK arena is still confusing to me and I am not very OOP literate.
Ohhh... Saints of Perl Wisdom, please bless this humble monk with guidance
zzSPECTREz
CODE
use Tk;
use TK::JPEG;
use Tk::Photo;
use strict;
use warnings;
sub mk_thumbnail {
my $win = shift;
my $img = shift;
my $photo = $win->Photo( -file => $img );
my $thumb = $win->Photo;
my $w = $photo->width;
my $h = $photo->height;
$w = ( $w /60 );
$h = ( $h /60 );
$thumb->copy( $photo, -subsample => ( $w, $h ) );
$photo->delete;
return $thumb;
}
my $mw = MainWindow->new;
my %images;
my ($row, $col) = ( 0,0 );
foreach my $img ( <*.jpg>, <*.bmp> ) {
my $thumb = mk_thumbnail ( $mw, $img );
$images{$img}=undef;
my $frame = $mw->Labelframe( -text => $img)->grid( -row => $row, -
+column => $col);
my $lb1 = $frame->Label ( -image => $thumb)->pack;
my $chk = $frame->Checkbutton ( -variable => \$images{$img} )->pac
+k;
$col++;
if ($col == 4) {
$col = 0;
$row++;
}
}
MainLoop;
foreach my $k ( keys %images ){
print "$k:";
if ($images{$k} ) {
print " selected.\n";
}else{
print " not selected.\n";
}
}
| [reply] [Watch: Dir/Any] [d/l] |
|
I guess TK isn't that popularNow, now, don't starting yelling things that are not true, just because someone dosn't give you an instant answer. You have asked a question, about doing something "the hard way", and most Tk programmers would not try to do it your way, by manipulating the Tk::Thumbnail module. I probably would
not use Tk::Thumbnail(as you have discovered), and instead make the thumbnails yourself, with Imager or ImageMagick. Then take the thumbnails and display them in an HList or TableMatrix, etc. I would prefer an HList in a left-frame, and the main display in the right frame. You can put as much as you want in an Hlist entry, thumbnails, checkboxes, selection highlighting, etc. In your second example, you seem to have found a way, (although there is no TK::JPEG), so what do you want to know? If you are looking for an example the way I would do it, look at ztkdb . Here is a screenshot . Now all you need to do is make the -selection option of the HList to be "multiple", and strip off all the database junk which you don't need, and display all selected thumbails in the right frame.
I'm not really a human, but I play one on earth.
flash japh
| [reply] [Watch: Dir/Any] |
|
>> I guess TK isn't that popular
> Now, now, don't start yelling things that are not true...
Oh... Just ignore that.. I sometimes forget sarcasm does not translate the same when written as opposed to spoken... It was ment more to be humourous... Usually responses on perlmonks seem to appear before I even submit the question.. :)
> In your second example, you seem to have found a way, ( although there is no TK::JPEG)...
TK::JPEG must be a windows Activestate thing. If I remove that use, the code will die with an error stating:
couldn't recognize data in image file "???@@@" at c:/Perl/site/lib/Tk/Image.pm line 21." So logicly you think I just need to send the -format option... You add the -format option then get an error: image file format "JPEG" is not supported at C:/Perl/site/lib/Tk/Image.pm line 21. Why this is the case I do not know... But add the use TK::JPEG and everything works fine not -format required. I'm really new to Tk so am just figuring things out.
> so what do you want to know?
EVERYTHING!! :) I'm just looking for ideas on how others would deal with the situation. It's not hard to steal acquire some code from a module and make it do what you want.. However, the all knowing powers that be will usually chastise you for reinventing the wheel and poorly at that. (Rightfully so).
So Im trying to see if there is a way to reuse Tk::Thumbnail but add the features I need to it. Seems like I should be able to make use of the label -command to just visually make it apparent which image was selected. Or even better, would be to make it do something like my second example and add checkboxes. I would think you would have to make a subclass of the initial module. I have not been able to figure out how to go about either of these ways. Maybee, I am going about it wrong, and just need to reinvent the whole thing like Im doing in my second example.
Thank you for your response and your suggestions! I wil have to look at your code ztkdb...
zzSPECTREz | [reply] [Watch: Dir/Any] |
|
|
Log In?
|
|
Domain Nodelet?
|
www.com | www.net | www.org
|
Node Status?
|
node history Node Type: perlquestion [id://447552] Approved by ghenry help
|
Chatterbox?
|
How do I use this? • Last hour • Other CB clients
|
Other Users?
|
Others learning in the Monastery: (5) As of 2024-03-29 13:13 GMT
|
Sections?
|
callback help
NODE.owner = 36692
N.title = Sections sitedoclet
N.owner = 17342
-->
|
Information?
|
callback help
NODE.owner = 36692
N.title = Information sitedoclet
N.owner = 17342
-->
|
Find Nodes?
|
callback help
NODE.owner = 36692
N.title = Find Nodes sitedoclet
N.owner = 17342
-->
|
Leftovers?
|
callback help
NODE.owner = 36692
N.title = Leftovers sitedoclet
N.owner = 17342
-->
|
Voting Booth?
|
No recent polls found
|
|