Re: button children!!

On Tue, 2003-01-28 at 10:58, Andy Ford wrote:
Thanks muppet - that is in fact what I did in the end - I became
extremely bored of looking for the correct method!!!

no problem!

Maybe you could answer this next one for me ....

I am trying to swap a pixmap with a new one every time a button is
clicked. Is this possible or will I need to use gtkImage??

it's possible.

i'm assuming you're using gtk-1.2 and GtkPerl-0.7whatever, not the
bleeding edge gtk2-perl.

this code isn't pretty or all that correct in terms of encapsulation,
but i tested it and it does indeed work.  the xpms i used are the zoom
in and zoom out icons from the gimp -- i.e., they are the same size.


#!/usr/bin/perl -w

use Gtk '-init';

# keep track of which image is current
$which = 1;

$window = new Gtk::Window 'toplevel';

# realize the window so we can create the pixmaps...
# could've gotten the colormap out of the widget instead,
# but switched to this when i discovered that create_from_xpm won't 
# take undef as the transparent color.

# create both pixmaps.
($pixmap1, $mask1) = 
                ( $window->window, undef, undef, "zoom_in.xpm" );
($pixmap2, $mask2) = 
                ( $window->window, undef, undef, "zoom_out.xpm" );

# this one is the Gtk::Pixmap widget we'll use to display the
# Gtk::Gdk::Pixmaps created above.
$pixmap = Gtk::Pixmap->new ( $pixmap1, $mask1 );

$label = Gtk::Label->new ("hi there");
$hbox = Gtk::HBox->new;
$button = Gtk::Button->new;
$window->add ($button);
$button->add ($hbox);
$hbox->pack_start ($pixmap, 1, 1, 1);
$hbox->pack_start ($label, 1, 1, 1);


$button->signal_connect ( clicked => sub {
                        # change images.
                        if ( $which ) {
                                $pixmap->set ( $pixmap2, $mask2 );
                        } else {
                                $pixmap->set ( $pixmap1, $mask1 );
                        # show the other image next time
                        $which = ! $which;
                } );


muppet <scott asofyet org>

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]