免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2208 | 回复: 0
打印 上一主题 下一主题

PyGtk之Fixed Container介绍 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-12 14:11 |只看该作者 |倒序浏览
也许程序就是这样,需要日积月累的熟悉语言的语法、内在API、数据结构以及优良的算法,然后才可以去解决实际问题,才可以谈创造力,否则就是空想了。正如我以前一直希望自己写程序、能够进入计算机世界,也就这样一直的在窥探,努力去看到本质。
今天要讲解的是pygtk中的一个小的容器--可实现动态调整位置。这就是fixed container,并通过解析pygtk tut上的一个例子去理解其功能,当然能举一反三是最好的了。
我们追溯到GTK+中的解释:
The GtkFixed widget is a type of layout container that allows you to place widgets by the pixel.
而Pygtk中是这样说的:
The Fixed container allows you to place widgets at a fixed position within it’s window,
relative to it’s upper left hand corner. The position of the widgets can be changed dynamically.
不管英语怎么说,也就是说只要我们在一个窗口中实现一个fixed容器就可以动态的调整这个容器中的widget,而具体的就是利用屏幕的x,y座标。
用法很简单:仅有三个相关的调用
fixed = gtk.Fixed()
fixed.put(widget, x, y)
fixed.move(widget, x, y)
The function gtk.Fixed() allows you to create a new Fixed container.  建立容器前的类的申明。
The put() method places widget in the container fixed at the position specified by x and y. 初始化的位置。
The move() method allows the specified widget to be moved to a new position.  相当于偏移量,呵呵。
让我们看下面的例子:(例子来源于pygtk的教程)
#!/usr/bin/env python
import pygtk
pygtk.require20
import gtk
class FixedExample:
    def move_button(self,widget):
        self.x = (self.x+60)%500  //不错的循环方式,值得学习。
        self.y = (self.y+50)%500
        self.fixed.move(widget,self.x,self.y)  //当接收到按钮的信号时,按钮移动到指定位置当计算结果大于500取余,则来次循环。
    def __init__(self):
        self.x = 80
        self.y = 80
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("hello fixed")
        window.connect("destroy",lambda w: gtk.main_quit())
        window.set_border_width(10)
        self.fixed = gtk.Fixed() //初始化
        window.add(self.fixed)
        self.fixed.show()
        for i in range(1,10)://一共建了9个按钮
            button = gtk.Button("touch me!!")
            button.connect("clicked",self.move_button)
            self.fixed.put(button,i*100,100) //将按钮放在横坐标为1*100,纵座标为100,
            button.show()
        window.show()
def main():
    gtk.main()
    return 0
if __name__ == "__main__":
    FixedExample()
    main()
看两张截图:
1、最初的:

2、点击了n次之后。

有兴趣的可以试验下其它的widget。
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/6303/showart_1409038.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP