Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1116710
  • 博文数量: 309
  • 博客积分: 6093
  • 博客等级: 准将
  • 技术积分: 3038
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-03 17:14
个人简介

linux学习记录

文章分类

全部博文(309)

文章存档

2014年(2)

2012年(37)

2011年(41)

2010年(87)

2009年(54)

2008年(88)

分类:

2009-02-25 16:50:52

什么是table attach了呢?先让我们运行一段代码:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class Table:
    def callback(self,widget,data=None):
        print "Hello again - %s was pressed" % data
    def delete_event(self,widget,event,data=None):
        gtk.main_quit()
        return False
   
    def __init__(self):
        self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Table")
        self.window.connect("delete_event",self.delete_event)
        self.window.set_border_width(20)
        table = gtk.Table(2,2,True)
        self.window.add(table)
        button = gtk.Button("One")
        button.connect("clicked",self.callback,"One")
        table.attach(button,0,1,0,2)
        button.show()
        button = gtk.Button("Two")
        button.connect("clicked",self.callback,"Twol")
        table.attach(button,1,2,0,1)
        button.show()
        button = gtk.Button("Quit")
        button.connect("clicked",lambda w: gtk.main_quit())
        table.attach(button,0,2,2,3)
        button.show()
        table.show()
        self.window.show()
def main():
    gtk.main()
    return 0
if __name__ == "__main__":
    Table()
    main()
将上述代码随意保存为一文件。用python执行:会看到如下的运行结果。
[img]http://blog.chinaunix.net/photo/6303_081023181535.png[/img]
那么决定它们的位置是此段代码中的是那句了呢?就是我标出来的红色的那三句。下面我来解释下此类的用法:
欲了解attach,必先说明Tables类,当然gtk.tables()函数更是必不可少的了。见下图即是一个2X2的表格
0                1             2
0+----------+----------+
|                  |               |
1+----------+----------+
|                  |               |
2+----------+----------+
原函数为:table = gtk.Table(rows=2, columns=2, homogeneous=False)
如果我们To place a widget into a box,那么我们就得使用attach.
原函数如下:
table.attach(child, left_attach, right_attach, top_attach, bottom_attach, xoptions=EXPAND|FILL, yoptions=EXPAND|FILL, xpadding=0, ypadding=0)
第一个参数"child“,也就是你需要往里面放的widget,在我们的例子中是一个字符串,如"One","Two","Quit".你也可以放置GTK中的其他Widget.
接下来的四个参数就是我要说的重点。分别为左、右、上、下。官方的文档并没有解释规则:比如左必须小于右,上必须小于下,而且不能相等(相等即覆盖),只 要知道了这样一个规则,那么其他的就随意了,反正就是那么一个表格,行、列所定的格。举个例子:比如我在3x3的表格里放置我上述代码中的三个 widget,而且大小相等,对角放置。那么我修改上述红色代码为:
table.attach(button,0,1,0,1)
table.attach(button,1,2,1,2)
table.attach(button,2,3,2,3)
改变后运行结果:
[img]http://blog.chinaunix.net/photo/6303_081023183603.png[/img]
那么,轻而易举的就可以将另外的对角写出来 。
table.attach(button,2,3,0,1)
table.attach(button,1,2,1,2)
table.attach(button,0,1,2,3)
有心的可以去验证一下:)
至于后面的参数,不常用,可以看pygtk的文档。
当然table还可以设定表格的行和列的空间大小。比如我在上述程序中添加如下两句话,然后运行结果:
table.set_row_spacings(50)
table.set_col_spacings(50)
[img]http://blog.chinaunix.net/photo/6303_081023184857.png[/img]
好了,那么表格还需要怎么定制?合并单元格?那么我们可以试验下。例如将One占所有第一行,Two占中间的列,Quit在原地扩大两倍。代码和截图如下:
table.attach(button,0,3,0,1)
table.attach(button,1,2,0,3)
table.attach(button,0,2,1,3)
[img]http://blog.chinaunix.net/photo/6303_081023185527.png[/img]
PyGtk是不是很简单易用阿?管中窥豹,只见一隅。Python和系统结合的地方还有更长的路要走。
               
阅读(1240) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~