user3389435
user3389435

Reputation:

How I can align widgets in GTK 3

box1=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,3);
align=gtk_alignment_new(1,0,0,0);
my=gtk_button_new_with_label("HELLO");
gtk_container_add(GTK_CONTAINER(frame),my);
gtk_container_add(GTK_CONTAINER(box1),frame);
gtk_container_add(GTK_CONTAINER(window),box1);

I must write all this code to align a button on the left up of the window or is a easier way.If someone can give me more information about containers of GTK because I understand hard the reference .

Before using Glade I want to see how things works. Sorry about my english.

Upvotes: 6

Views: 13274

Answers (2)

ohmu
ohmu

Reputation: 19752

UPDATE: As noted by jku, GtkHBox, GtkVBox, and GtkTable are now deprecated, and GtkGrid should be used instead. GtkAlignment is also deprecated, and the properties given in the answer by Phillip Wood should be used instead.


I think you meant gtk_alignment_new(0,0,0,0) to align to the left. But without knowing about the other widgets within the window, that looks good to me. That's how you align.

The basic layout containers in GTK are:

  • GtkFixed which allows widgets to be absolutely positioned.

    +----------------------------+
    |                   Widget 3 |
    | Widget 2                   |
    |              Widget 1      |
    |                            |
    +----------------------------+
    
  • GtkHBox allows widgets the be positioned horizontally:

    +--------+-----+
    | Widget | ... |
    +--------+-----+
    
  • GtkVBox allows widgets to be positioned vertically:

    +--------+
    | Widget |
    +--------+
    |  ...   |
    +--------+
    
  • GtkTable allows for a grid or table layout.

    +--------+-----+
    | Widget | ... |
    +--------+-----+
    |  ...   | ... |
    +--------+-----+
    

In addition to those containers, there's also GtkAlignment which allows you to control the alignment of a widget within its allotted space.

GTK+ layout management is a decent tutorial providing working examples of the different layout containers.

Upvotes: 4

Phillip Wood
Phillip Wood

Reputation: 841

As the documentation for GtkAlignment notes in Gtk3 there is usually no need to use it as the same effect can be achieved by setting the halign and margin, hexpand properties of the widget that you wish to align.

To have the button in the top left you would set halign and valign to GTK_ALIGN_START.

Upvotes: 12

Related Questions