Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1728386
  • 博文数量: 42
  • 博客积分: 10036
  • 博客等级: 上将
  • 技术积分: 2285
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 17:08
文章存档

2011年(3)

2010年(3)

2009年(5)

2008年(31)

分类: Python/Ruby

2008-11-12 14:11: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。
阅读(1818) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~