Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: GTK2 - Load updated image file into a frame

by zentara (Archbishop)
on Jun 07, 2012 at 21:32 UTC ( [id://975049]=note: print w/replies, xml ) Need Help??


in reply to GTK2 - Load updated image file into a frame

Hi,

To amplify on what Anonymous Monk said, here is how to do it. You do not want to rebuild the $hbox, only the image. I also cleaned up your code, you had many useless lines. The script below will cycle thru all jpg's in the script's directory for the demo. I improved on your use of "cat" to load the file, that code looked familiar to me, and is not very advanced. :-) The code below is much better, and it can be improved a bit, but it does show you the idea.

#!/usr/bin/perl -w use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use strict; # load all jpgs in directory for this demo my @files = <*.jpg>; #print "@files\n"; my $window = Gtk2::Window->new('toplevel'); $window->signal_connect(delete_event => sub { Gtk2->main_quit; return +FALSE; }); $window->set_title("Test set up"); $window->set_border_width(0); # create your first image and pass it to $hbox my $img1 = load_first_image(); my $hbox = &make_gui_frame($img1); $window->add($hbox); Glib::Timeout->add(1000, \&update_image); $window->show; Gtk2->main; 0; sub load_first_image{ my $file = $files[0]; my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,300,3 +00,1); return Gtk2::Image->new_from_pixbuf($pixbuf); } sub update_image { # circular list of images just for demo push (@files,shift(@files)); my $file = $files[0]; my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,300,300 +,1); $img1->set_from_pixbuf ($pixbuf); $window->show_all(); return 1; } sub make_gui_frame { my $img1 = shift; ## MAIN HBOX $hbox = Gtk2::HBox->new(FALSE, 20); my $frame = Gtk2::Frame->new('Data control'); $frame->set_shadow_type ('out'); #method of Gtk2::Container $frame->set_border_width(5); my $box1 = Gtk2::VBox->new(FALSE, 10); my $box2 = Gtk2::VBox->new(FALSE, 10); $box2->set_border_width(10); $box1->pack_start($box2, TRUE, TRUE, 0); my $button = Gtk2::Button->new("close"); $button->signal_connect(clicked => sub { Gtk2->main_quit; }); $box2->pack_start($button, TRUE, TRUE, 0); $button->can_default(TRUE); $frame->add($box1); $hbox->pack_start($frame, FALSE, FALSE,0); $frame = Gtk2::Frame->new('Data output and Overlay'); $frame->set_shadow_type ('out'); #method of Gtk2::Container $frame->set_border_width(5); my $vbox_image = Gtk2::VBox->new(FALSE, 10); $vbox_image->pack_start($img1,FALSE,FALSE,0); $frame->add($vbox_image); $hbox->pack_start($frame, FALSE, FALSE,0); $hbox->show_all(); return $hbox; }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: GTK2 - Load updated image file into a frame
by seoseoboyle (Initiate) on Jun 12, 2012 at 20:05 UTC
    That worked a treat. Thanks for you help. The line $window->show_all() in sub update_image() was not obvious from the documentation on the web. This would be a good example. Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://975049]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found