含有章节索引的中文 文章模板

::-- ehu4ever [2005-08-08 13:47:49]

1. 表格方式排版

让我们再来见识一下另外一种packing的方法——网格,有时候他也很管用喔。

我们可以用一张网格的一个个方格来安排widget的位置,widget将会尽可能多地占用分配给它的空间。

首先要注意的当然应该是gtk.Table:

  table = gtk.Table(rows=1, columns=1, homogeneous=False)

前两个参数是网格的行数和列数。

参数homogeneous决定了网格的方格的大小,如果是True,则网格中的所有方格都为网格中最大widget的大小,如果是False,网格中某一个方格的高是那一行中最高的widget的高,它的宽是那一列中最宽的widget的宽。

网格的行数和列数是从0开始计数的,行数和列数在网格定义的时候确定,所以gtk.Table(2, 2)的结果是这样的:

   0          1          2
  0+----------+----------+
   |          |          |
  1+----------+----------+
   |          |          |
  2+----------+----------+

这个二次元系统是从左上角开始计数的。我们用下面的方法把widget放入网格:

  table.attach(child, left_attach, right_attach, top_attach, bottom_attach,
               xoptions=EXPAND|FILL, yoptions=EXPAND|FILL, xpadding=0, ypadding=0)

意思是把child放入table,left_attach, right_attach, top_attach, bottom_attach这四个参数是确定child放在什么位置以及占用几个方格。如果你想把child放在一个2×2网格的右下角,并且占用一个方格,则应该这样设置:left_attach = 1, right_attach = 2, top_attach = 1, bottom_attach = 2。 如果你想让child占用2×2网格的最上一行,则应该这样:left_attach = 0, right_attach = 2, top_attach = 0, bottom_attach = 1。

xoptions和yoptions用来确定各种packing的选项,它们可以用OR来组织多个选项。 这些选项有:

FILL

If the table cell is larger than the widget, and FILL is specified, the widget will expand to use all the room available in the cell.

SHRINK

If the table widget was allocated less space then was requested (usually by the user resizing the window), then the widgets would normally just be pushed off the bottom of the window and disappear. If SHRINK is specified, the widgets will shrink with the table.

EXPAND

This will cause the table cell to expand to use up any remaining space allocated to the table.

padding参数其实和box里的差不多,它确定了widget周围的空间。

我们可以用 set_row_spacing()或set_col_spacing()来确定行与行或是列与列之间的空间。

  table.set_row_spacing(row, spacing)

  table.set_col_spacing(column, spacing)

这个空间在行的下面或是在列的右边。

也可以同时确定所有列或是所有行之间的空间:

  table.set_row_spacings(spacing)

  table.set_col_spacings(spacing)

最后还要提一下,这样东西对于最后一行和最后一列是没有什么作用的。