John Hunter <[EMAIL PROTECTED]> writes:

> How can I control the alignment of widgets (eg labels or checkbuttons)
> in a table?  All of the widgets in a single column are of the same
> type.  Is it possible to get them, for example, to left or right
> align?
> 
>     table = gtk.Table(len(keys),2)
>     table.set_col_spacings(3)
>     table.set_row_spacings(3)
>     table.show()
> 
>     label = gtk.Label(name)
>     label.show()
>     #label.set_alignment(xalign=1, yalign=0.5)
>     entry = gtk.Entry()
>     entry.show()
>     table.attach(label, 0, 1, 0, 1, xoptions=gtk.TRUE, yoptions=gtk.TRUE)
>     table.attach(entry, 1, 2, 0, 1, xoptions=gtk.TRUE, yoptions=gtk.TRUE)
> 
> Typically I do something like the above.  For labels, I was hopeful
> that the set_alignment would help but it did not.  But since I have
> other widgets (eg, checkbuttons) in other tables, I am looking for a
> general solution.

You don't want to write xoptions=gtk.TRUE.  The xoptions and yoptions
arguments are a bitmask of enum GtkAttachOptions that control what
happens when the widget is resized.  Valid options are 0 or any
bitwise combination of gtk.EXPAND, gtk.SHRINK and gtk.FILL.  Using
gtk.TRUE is the same as specifying gtk.EXPAND (purely by coincidence),
and I don't think this is what you want.  (Filling without expanding
is useful, but expand alone without fill is normally not desired.  In
the case of tables, expanding without filling results in centering the
widget regardless of any alignment you have on it.)  I think you want
to fill the left column and fill and expand the right column.  Try
this instead:


    table = gtk.Table(4, 2)
    table.set_col_spacings(3)
    table.set_row_spacings(3)
    table.show()

    label = gtk.Label('Testing label alignments in a table')
    label.show()
    label.set_alignment(xalign=0.0, yalign=0.5)
    entry = gtk.Entry()
    entry.show()

    table.attach(label, 0, 1, 0, 1, xoptions=gtk.FILL, yoptions=0)
    table.attach(entry, 1, 2, 0, 1, xoptions=gtk.EXPAND|gtk.FILL, yoptions=0)

    ll = gtk.Label('left align')
    lc = gtk.Label('center')
    lr = gtk.Label('right align')
    ll.show()
    lc.show()
    lr.show()

    ll.set_alignment(xalign=0.0, yalign=0.5)
    lc.set_alignment(xalign=0.5, yalign=0.5)
    lr.set_alignment(xalign=1.0, yalign=0.5)

    table.attach(ll, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=0)
    table.attach(lc, 0, 1, 2, 3, xoptions=gtk.FILL, yoptions=0)
    table.attach(lr, 0, 1, 3, 4, xoptions=gtk.FILL, yoptions=0)


Try resizing the window to see if it behaves the way you like.  In
this case the defaults provided by omitting all the xoptions and
yoptions might also work for you.  I think the only difference between
the options I used above and the default options is the default uses
yoptions=gtk.EXPAND.  (I don't like yoptions=gtk.EXPAND for labels and
entries, but you want to y-expand multiline widgets like scrolled
windows.)

This works for gtk.Label because it gets alignment options from
gtk.Misc.  For arbitrary widgets, I think Christian is right about
needing gtk.Alignment.
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to