Re: Why can't I create buttons in a for loop?



One of the tricky things to remember with Gtk is that a container can
(generally) contain only one widget.  Because of this, you need to use a
container of some sort within the main window.  HBox and VBox are
perfect for what you're trying to do here.  See the code below for how
to fix the example script.  (It's just the snippet, the rest is the
same).

Check out
http://personal.riverusers.com/~swilhelm/gtkperl-tutorial/box.html
or http://www.gtk.org/tutorial/ch-packingwidgets.html
for a much better explanation.

Here's basically what I added:  Create an HBox (or a Vbox if you choose
;), Within the for loop pack the button widget into the HBox then show 
it, after the for loop show the HBox, then pack it into the main window.

Btw,  I took a quick look at your site.. and just let me say that look
sweeeet!!! =)  I'll be taking a better look at it later.

------------------------
my $hbox = new Gtk::HBox(0,0);

for ($i = 0 ; $i < 5 ; $i++) {
    $button[ $i ] = new Gtk::Button($pornstar[$i]);
    $button[ $i ]->signal_connect("clicked", \&CloseAppWindow);
    $hbox->pack_start($button[$i],0,0,0);
    $button[ $i ]->show();
}

$hbox->show();
$window->add($hbox);

$window->show();
---------------------


On Sat, 2001-11-03 at 21:00, Christopher Bergeron wrote:
Does anyone know why the code posted below works out of a "for" loop but
does not work with the "for" loop?  I can execute the proggy fine with the
"for begin" and "for end" commented out, but not with them in.  Does anyone
have any suggestions?  I'm new to gtk but not necessarily perl.  For anyone
interested, my application is http://www.dashpc.com

Thanks very much in advance...!
-CB



#!/usr/bin/perl -w

use Gtk;         # load the Gtk-Perl module
use strict;      # a good idea for all non-trivial Perl scripts
set_locale Gtk;  # internationalize
init Gtk;        # initialize Gtk-Perl

my $window = new Gtk::Window( "toplevel" );
my $bw=5;

# callback registration
$window->signal_connect("delete_event", \&CloseAppWindow);
$window->border_width( $bw );

my @pornstar=("a","b","c","d","e","f","g","h","i","j");
my $i=0;
my @button=("1","2","3","4","5");

#doesn't work unless i comment the line below  and   ----------------!
for ($i=0;$i<5;$i++) {
  $button[$i] = new Gtk::Button("$pornstar[$i]");
  $button[$i]->signal_connect("clicked", \&CloseAppWindow);
  $button[$i]->show();
  $window->add($button[$i]);
#the line below also....  (basically, I can't do this in a loop,  whY?
}

$window->show();
main Gtk;

sub CloseAppWindow
{
   Gtk->exit(0);
   return 0;
}

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

-- 
@j=qw{-.2-2.- -2.4- -3.2-. -2.-.-2};
print map{s e(.)(\d)e$1x$2eeg;y.-.1.;y-.-0-;
chr unpack"N",pack"B32",substr'0'x32 .$_,-32} j;




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