Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi
i'm trying to learn perl gtk2 ,in the tutorials examples there is no description on how to position the widgets in a specific x,y coordinates. the example below from the tutorial have a button, text entry, label,
and as an example for demo purposes and in the main window which have the size (500,400) pixels how can i put the button on (x,y)=(30,50) pixels relative to the main window with size (30,20),
label on (x,y)=(200,50) size (100,50) text entry on (x,y) =(30,300)
best wishes for all
use Gtk2 '-init'; my $window = Gtk2::Window->new; $window->set_size_request(500,400); $window->set_position('center'); $window->set_title ("Hello world"); $window->signal_connect (destroy => sub { Gtk2->main_quit; }); my $vbox = Gtk2::VBox->new(); $vbox->set("border_width"=> 10); $window->add($vbox); my $label = Gtk2::Label->new("hello world"); $vbox->pack_start($label,0,0,5); # expand?, fill?, padding my $entry = Gtk2::Entry->new(); $vbox->pack_start($entry,0,0,5); my $button = Gtk2::Widget->new("Gtk2::Button", label=>"exit"); $button->signal_connect(clicked=>\&my_quit); $vbox->pack_start($button, 0,0,5); $window->show_all(); Gtk2->main; sub my_quit { print "bye!\n"; exit; }

Replies are listed 'Best First'.
Re: gtk2 widgets positions
by zentara (Cardinal) on Dec 15, 2009 at 12:50 UTC
    .... i don't know if its the best way..... you may want to use deeply nested hbox's and vbox's , which are created with a size_request....but see how i slide the horses in Gtk2 ....needs thread testing on a multi-threaded computer you use
    $widget->set_child_packing ($child, $expand, $fill, $padding, $pack_t +ype)
    it's in perldoc Gtk2::Box ...... also check out perldoc Gtk2::Widget..... it has many methods that almost all widgets inherhit ...... the way to search for methods, is read the perldoc, say perldoc Gtk2::Hbox.... at the top of the Hbox docs is the heirarchy tree..... if you don't find the method or signal in the perldoc you opened, keep going up thru the heirarchy and keep looking...... you eventually come to Gtk2::Box

    ... here is your code, with a nested hbox just to hold the label at pos 50..... you can get very creative with deep nesting of hbox's and vbox's

    P.S. you can also use a Gtk2::Table quite imaginatively

    #!/usr/bin/perl use warnings; use strict; use Gtk2 '-init'; my $window = Gtk2::Window->new; $window->set_size_request(500,400); $window->set_position('center'); $window->set_title ("Hello world"); $window->signal_connect (destroy => sub { Gtk2->main_quit; }); my $vbox = Gtk2::VBox->new(); $vbox->set("border_width"=> 10); $window->add($vbox); my $hbox1 = Gtk2::HBox->new(); $vbox->pack_start($hbox1,0,0,0); my $label = Gtk2::Label->new("hello world"); $hbox1->pack_start($label,0,0,50); # expand?, fill?, padding my $entry = Gtk2::Entry->new(); $vbox->pack_start($entry,0,0,5); my $button = Gtk2::Widget->new("Gtk2::Button", label=>"exit"); $button->signal_connect(clicked=>\&my_quit); $vbox->pack_start($button, 0,0,5); $window->show_all(); Gtk2->main; sub my_quit { print "bye!\n"; exit; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: gtk2 widgets positions
by biohisham (Priest) on Dec 15, 2009 at 09:38 UTC
Re: gtk2 widgets positions
by doug (Pilgrim) on Dec 15, 2009 at 14:42 UTC

    I know this isn't what you're asking, but my advice is to avoid fixed pixel offsets. I learned that lesson with Motif and I live by it today.

    The problem is if you switch to a different sized monitor, different resolution, or any of a bunch of user configuration items, what was a carefully crafted layout becomes an ugly jumble. Put minimum sizes for things that are fixed (say an image), but let everything else flow with the shape of the controlling window. You never can tell what the user, and his window manager, are going to do.

    Of course, if this is just for your personal use, do what you want. Toy projects usually have friendly users, so there is less of a problem.

    - doug

      avoid fixed pixel offsets

      .... yeah.... i think what happens is people learn fixed positioning in things like Tk place() or grid(),(or your Motif) and expect it to be there in more advanced toolkits.....like you said....it's really bad for resizing

      ...fwiw, you can control the exact position of a widget in a Gtk2 container, but you need to delve into the low-level Gdk window.... and then there are all sorts of refresh problems.... but see Gtk2 low-level icon for how it's basically done..... you could use an expose callback of sorts to get around refresh.....

      ....but generally the idea is to use the container packing they provide..... it's just a different way of looking at things..... but it works quite well..... after you construct 10 or 20 apps you get the hang of it :-)

      ..... in a fancy design where you want aligned buttons and widgets, the Gtk2::Table is the way to go.....just fill in empty table corners with wallpaper....i.e. empty labels or tiled images


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