pygtk入门(2)-第二个窗口

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-



import pygtk
pygtk.require('2.0')
import gtk

class DeepFuture:
    def blogurlshow(self,widget,data=None):
        print("我的博客:deepfuture.javaeye.com")
    def delwin_event(self,widget,event,data=None):
        #如果你在"delete_event"事件处理中,返回FALSE,GTK会继续发射这个信号,以供destroy,否则返回TRUE,表示这个信号已经被处理,不需要再处理了,也不需要再发射这个信号了,这意味着你不能destroy窗口。
        print("delete event!")
        return False
    def destroy_win(self,widget,data=None):
        #处理窗口销毁信号
        print("destroy event!")        
        gtk.main_quit()
    def __init__(self):
        #创建一个新窗口
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        #指定信号与相关处理事件
        self.window.connect("delete_event", self.delwin_event)			
        self.window.connect("destroy", self.destroy_win)
        self.window.set_border_width(15)
        self.button = gtk.Button("显示我的博客")
        self.button.connect("clicked", self.blogurlshow, None)
        self.button.connect_object("clicked", gtk.Widget.destroy,self.window)
        self.window.add(self.button)
        #显示按钮和相关窗口
        self.button.show()
        self.window.show()
    def main(self):
        gtk.main()
if __name__ == "__main__":	
	deep=DeepFuture()
	deep.main()
			

 
object是GtkWidget的被释放信号的实例, name是要捕捉的信号名称 func是处理信号的函数, func_data是传递给函数的数据,handler_id 可以断开或阻塞这个处理

2)def callback_func(widget, callback_data):回调函数

第一参数是指向发送信号的widget,第二个参数callback_data指向connect() 方法的最后一个数据,如果回调函数是一个对象的方法,则使用

def callback_meth(self, widget, callback_data)

self是这个对象的实例包括的方法。

3)handler_id = object.connect_object(name, func, slot_object)

connect_object()和connect()相同,但回调函数仅使用一个参数和一个回调函数:

def callback_func(object)
def callback_meth(self, object)

通常对象为widget时,connect_object() 允许pygtk widget方法使用单个参数argument (self)处理信号





 

"delete_event" 和 "destroy" 信号被捕捉,"delete_event" 在我们使用窗口管理器关闭这个窗口后,或我们调用GtkWidget destroy() 后产生,delete_event"处理中,返回 FALSE,"destroy" 被捕捉:

self.window.connect("delete_event", self.delete_event)

self.window.connect("destroy", self.destroy)



输出:

我的博客:deepfuture.javaeye.com

destroy event!


 

 

GTK is essentially an object oriented application programmers interface (API). Although written completely in C, it is implemented using the idea of classes and callback functions (pointers to functions). There is also a third component called GLib which contains a few replacements for some standard calls, as well as some additional functions for handling linked lists, etc. The replacement functions are used to increase GTK’s portability, as some of the functions implemented here are not available or are nonstandard on other unixes such as g_strerror(). Some also contain enhancements to the libc versions, such as g_malloc that has enhanced debugging utilities. In version 2.0, GLib has picked up the type system which forms the foundation for GTK’s class hierarchy, the signal system which is used throughout GTK, a thread API which abstracts the different native thread APIs of the various platforms and a facility for loading modules. As the last component, GTK uses the Pango library for internationalized text output. This tutorial describes the Python interface to GTK+ and is based on the GTK+ 2.0 Tutorial written by Tony Gale and Ian Main. This tutorial attempts to document as much as possible of PyGTK, but is by no means complete. This tutorial assumes some understanding of Python, and how to create and run Python programs. If you are not familiar with Python, please read the Python Tutorial [http://www.python.org/doc/current/tut/tut.html] first. This tutorial does not assume an understanding of GTK; if you are learning PyGTK to learn GTK, please comment on how you found this tutorial, and what you had trouble with. This tutorial does not describe how to compile or install Python, GTK+ or PyGTK. This tutorial is based on: • GTK+ 2.0 through GTK+ 2.4 • Python 2.2PyGTK 2.0 through PyGTK 2.4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值