Changes

Jump to navigation Jump to search
3,081 bytes added ,  13:47, 10 November 2011
Line 113: Line 113:  
Combining the two points above, if you wish to call a construct-like function such as gtk_button_new_with_label(), you do have the option of calling Gtk.Button.new_with_label(), however if we check the [http://developer.gnome.org/gtk3/3.2/GtkButton.html#GtkButton.properties GtkButton properties] we see one called "label" which is equivalent. Therefore gtk_button_new_with_label("foo") should be called as:
 
Combining the two points above, if you wish to call a construct-like function such as gtk_button_new_with_label(), you do have the option of calling Gtk.Button.new_with_label(), however if we check the [http://developer.gnome.org/gtk3/3.2/GtkButton.html#GtkButton.properties GtkButton properties] we see one called "label" which is equivalent. Therefore gtk_button_new_with_label("foo") should be called as:
 
  button = Gtk.Button(label="foo")
 
  button = Gtk.Button(label="foo")
 +
 +
=== HBox, VBox, pack_start and pack_end ===
 +
 +
GtkHBox and GtkVBox, commonly used containers in GTK2 code, have pack_start and pack_end methods. These take 4 parameters:
 +
# The widget to pack into the container
 +
# '''expand''': Whether the child should receive extra space when the container grows (default True)
 +
# '''fill''': True if space given to child by the expand option is actually allocated to child, rather than just padding it. This parameter has no effect if expand is set to False. A child is always allocated the full height of a gtk.HBox and the full width of a gtk.VBox. This option affects the other dimension. (default True)
 +
# '''padding''': extra space in pixels to put between child and its neighbor (default 0)
 +
 +
In PyGTK, the expand, fill and padding parameters were optional: if unspecified, the default values above were used. In PyGI, these parameters are '''not''' optional: all 4 must be specified. Hence the rules for adding in the extra parameters are:
 +
 +
# If '''expand''' was not set, use value True
 +
# If '''fill''' was not set, use value True. (however, if expand is False, this parameter gets ignored so False is an equally acceptable option when expand=False)
 +
# If padding was not set, use value 0.
 +
 +
These parameters can be specified either as positional arguments or as named keyword arguments, however all 4 must always be specified. Some developers prefer keyword arguments, arguing that the following:
 +
<pre>box.pack_start(widget, expand=True, fill=False, padding=4)</pre>
 +
is much more readable than:
 +
<pre>box.pack_start(widget, True, False, 4)</pre>
 +
 +
However, these functions are called extremely often; any mildly seasoned GTK developer will have memorized the order and meaning of the parameters. Some developers therefore prefer to avoid the extra work of dropping in hundreds of keyword arguments throughout the code and just use the positional ones. This is really up to you.
 +
 +
If you are using pack_start with the default values (expand=True, fill=True and padding=0), you can avoid using pack_start (and the parameter pain that it brings with it) by just using .add for some added cleanliness, e.g.
 +
<pre>box.pack_start(widget, True, True, 0)</pre>
 +
can be replaced with:
 +
<pre>box.add(widget)</pre>
 +
 +
This is as far as you need to go for now. However, in GTK3, GtkVBox and GtkHBox have been deprecated, which means they might be removed in GTK4. The replacement is to use GtkBox directly, and you may wish to make this change now. e.g.:
 +
<pre>vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)</pre>
 +
 +
However, it must be noted that if GtkBox is used directly (instead of using GtkHBox/GtkVBox), the default value of '''expand''' is now '''False'''. The implications of this are:
 +
# You need to check your .add() calls, as previously they would behave as pack_start with expand=True, but now they will behave as expand=False (you need to change them to use pack_start with expand=True to retain the old behaviour)
 +
# Every single pack_start call that has expand=False and padding=0 (and any value of fill) can be converted to .add() for cleanliness
    
==Make a release==
 
==Make a release==
105

edits

Navigation menu