Home > Archive > PerlTk > September 2004 > changing images?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Pepper Orlando 2004-09-20, 4:01 pm |
| I need help! How do I display a JPEG image/photo in a Perl/Tk app such
that it can easily be changed during runtime?
I have used Perl/Tk lightly for the past year, almost all of my apps
have used a very basic GUI. A few of my apps have had a single
-static- image (typically a logo in the corner of the window). I am
now trying to add an "image viewer" to one of my apps, but it's giving
me all sorts of problems. Either the image loads and can't be changed
later on, or it won't load at all. Despite it being a simple example,
I don't really understand the code for the image viewer demo in the
widget demo collection.
I basiclly want to be able to cycle through a few images. The sample
app I have been trying to write (to debug my mess) is supposed to be
just one window containing two items: an image and a "next" button.
The names of the images (image1.jpg, image2.jpg, etc) reside in a
list. I want the demo app to display the first image in the list upon
launch, then change/update the image to the next one in the list when
the "next" button is clicked.
Can anyone point me to some very simple sample code for what I am
trying to accomplish? I'm not much of a programmer (especially when it
comes to GUI), but I'm trying to learn. I am totally stuck on this
one!
| |
| Vorxion 2004-09-21, 9:01 am |
| In article <c00eb680.0409200655.161cebb@posting.google.com>, Pepper Orlando wrote:
>I need help! How do I display a JPEG image/photo in a Perl/Tk app such
>that it can easily be changed during runtime?
>
>I have used Perl/Tk lightly for the past year, almost all of my apps
>have used a very basic GUI. A few of my apps have had a single
>-static- image (typically a logo in the corner of the window). I am
>now trying to add an "image viewer" to one of my apps, but it's giving
>me all sorts of problems. Either the image loads and can't be changed
>later on, or it won't load at all. Despite it being a simple example,
>I don't really understand the code for the image viewer demo in the
>widget demo collection.
>
>I basiclly want to be able to cycle through a few images. The sample
>app I have been trying to write (to debug my mess) is supposed to be
>just one window containing two items: an image and a "next" button.
>The names of the images (image1.jpg, image2.jpg, etc) reside in a
>list. I want the demo app to display the first image in the list upon
>launch, then change/update the image to the next one in the list when
>the "next" button is clicked.
>
>Can anyone point me to some very simple sample code for what I am
>trying to accomplish? I'm not much of a programmer (especially when it
>comes to GUI), but I'm trying to learn. I am totally stuck on this
>one!
Well, given this:
$photo = $pframe->Photo(-file => 'Thumbnails/startup.jpg');
$pinner = $pframe->Frame(-width => 135, -height => 200, -background => '#30
3030')->pack(-side => 'right', -anchor => 'center', -expand => 1, -fill => 'x');
$panelabel = $pinner->Label(-font => 'panelabel', -text => 'Preview', -fore
ground => '#FFFFFF', -background => '#303030')->pack(-side => 'top', -anchor =>
'center', -expand => 1, -fill => 'none', -padx => 5, -pady => 5);
$pane = $pinner->Label(-image => $photo, -width => 128, -height => 128, -ba
ckground => '#000000', -relief => 'raised')->pack(-side => 'top', -anchor => 'ce
nter', -expand => 1, -fill => 'none', -padx => 5, -pady => 5);
I can tell you that $photo is my actual Photo object, $pinner is a frame
inside another frame, $panelabel is just a label that says "Preview" above
my $pane, which is the actual label that gets the Photo object with the
-image attibute.
When I want to switch images, I simply do this:
$thumbname = '/some/path/to/file.jpg'; # NOT the actual code :)
$photo = $pframe->Photo(-file => ${thumbname});
$pane->configure(-image => ${photo});
$mw->update;
The $mw is my MainWindow object.
You just reset the photo object, reconfigure the label with the new photo
object, and update.
I use this in a rollover sense against a list of documents, all of which
have thumbnail images for the craft patterns they display if you launch the
real web browser from the anchor in the ROText object I use. As I have it
bound, every time there's a movement that changes which selection is
presently highlighted, I change the image on the fly. Very fast, very
easy. Works in realtime with 128x128 JPEG images on a P166, scrolling
straight down over a list of about 25-30 at a time that are visible at any
given time in the list, to give you an idea.
Hope this helps you.
--
Vorxion - Founder of the knocking-shop of the mind.
"You have it, you sell it, you've still got it--what's the difference?"
--Diana Trent, "Waiting for God", on why a modelling agency is really a
knocking-shop. Applied by me to the field of consulting. :)
The Sci-Fi fan's solution to debt: Reverse the polarity on your charge card.
| |
| chaosppp 2004-09-22, 8:59 pm |
| Pepper Orlando wrote:
> I need help! How do I display a JPEG image/photo in a Perl/Tk app such
> that it can easily be changed during runtime?
>
> I have used Perl/Tk lightly for the past year, almost all of my apps
> have used a very basic GUI. A few of my apps have had a single
> -static- image (typically a logo in the corner of the window). I am
> now trying to add an "image viewer" to one of my apps, but it's giving
> me all sorts of problems. Either the image loads and can't be changed
> later on, or it won't load at all. Despite it being a simple example,
> I don't really understand the code for the image viewer demo in the
> widget demo collection.
>
> I basiclly want to be able to cycle through a few images. The sample
> app I have been trying to write (to debug my mess) is supposed to be
> just one window containing two items: an image and a "next" button.
> The names of the images (image1.jpg, image2.jpg, etc) reside in a
> list. I want the demo app to display the first image in the list upon
> launch, then change/update the image to the next one in the list when
> the "next" button is clicked.
>
> Can anyone point me to some very simple sample code for what I am
> trying to accomplish? I'm not much of a programmer (especially when it
> comes to GUI), but I'm trying to learn. I am totally stuck on this
> one!
Sounds similar to what this script I re-wrote a few nights ago is doing.
It makes an array from the jpg files in a directory and then cycles
through them. The cycle speed can be adjusted in the app.
Phil - phil681 at yahoo
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::Photo;
use Tk::JPEG;
my (@files,@path,$c);
my $main = new MainWindow;
my $canvas = $main ->Canvas('-width'=>800,
'-height'=>600)->pack;
my $label = $main->Label()->pack;
my $label2 = $main->Label()->pack;
my $button1 = $main->Button(-text => 'faster',
-command => \&faster)->pack;
my $button2 = $main->Button(-text => 'slower',
-command => \&slower)->pack;
load_images(0);
my $img=$canvas->Photo( '-format' => 'jpeg' ,
'-file' => $path[0] );
my $id=$canvas->createImage( 0, 0, '-anchor' => 'nw',
'-image', => $img );
my $rpt=2000;
my $min=150; # increase this if it starts to freeze at fastest setting
my $max=5000; # increase if you want more time per image
my $incdec=50; # default 50ms increase/decrease
my $show=0; # start with 0 index'd image
my $time_this=$canvas->repeat($rpt,\&show_pic);
$label->configure(-text => $path[0]);
$label2->configure(-text => $rpt);
MainLoop;
sub slower {
return if $rpt>$max;
$rpt+=$incdec;
$time_this->time($rpt);
$label2->configure(-text => $rpt);
}
sub faster {
return if $rpt<$min;
$rpt-=$incdec;
$time_this->time($rpt);
$label2->configure(-text => $rpt);
}
sub show_pic {
$show++;
if ($show == $c){$show=0}
$img->read($path[$show]);
$label->configure(-text => $path[$show]);
}
sub load_images {
$c=shift;
my $dir="./pics";
my $image;
opendir(DIR,$dir)or die "<failed to open $dir> $!\n";
@files=sort readdir(DIR);
foreach (@files) {
next unless (/\.jpg/);
$path[$c] = "$dir/$_";
$c++;
}
}
|
|
|
|
|