Python

http://www.pygtk.org/pygtk2tutorial/


核心问题

  • GTK是一个事件驱动的框架,也就是说他会一直在gtk.main()里面运行,直到有某种合适的事件发生。
handler_id = object.connect(name, func, func_data)
# name 是 signal 的名称,func 是你要绑定的程序,func_data 是你要传递的数据,handler_id 可以用来关闭或者阻止绑定。
# connect() 也可以用来绑定 X 本身的一些 event,比如 delete_event
def callback_func(widget, callback_data):
# widget 是发出信号的组件,
handler_id = object.connect_object(name, func, slot_object)
# connect_object 允许只有一个参数的传递。

hello world

# 导入相关库
import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:

    # This is a callback function. The data arguments are ignored
    # in this example. More on callbacks below.
    # 绑定 callback function
    def hello(self, widget, data=None):
        print "Hello World"

    def delete_event(self, widget, event, data=None):
        # delete_event 是一个 event?还是 signal handler
        # If you return FALSE in the "delete_event" signal handler,
        # GTK will emit the "destroy" signal. Returning TRUE means
        # you don't want the window to be destroyed.
        # This is useful for popping up 'are you sure you want to quit?'
        # type dialogs.
        print "delete event occurred"

        # Change FALSE to TRUE and the main window will not be destroyed
        # with a "delete_event".
        return False

    def destroy(self, widget, data=None):
        # 这个是用来退出的
        print "destroy signal occurred"
        gtk.main_quit()

    def __init__(self):
        # 创造窗口
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        # When the window is given the "delete_event" signal (this is given
        # by the window manager, usually by the "close" option, or on the
        # titlebar), we ask it to call the delete_event () function
        # as defined above. The data passed to the callback
        # function is NULL and is ignored in the callback function.
        # 将 delete_event 这个信号与 self.delete_event 这个功能绑定
        self.window.connect("delete_event", self.delete_event)

        # Here we connect the "destroy" event to a signal handler.  
        # This event occurs when we call gtk_widget_destroy() on the window,
        # or if we return FALSE in the "delete_event" callback.
        self.window.connect("destroy", self.destroy)

        # Sets the border width of the window.
        self.window.set_border_width(10)

        # Creates a new button with the label "Hello World".
        self.button = gtk.Button("Hello World")

        # When the button receives the "clicked" signal, it will call the
        # function hello() passing it None as its argument.  The hello()
        # function is defined above.
        # 将 button 的 clicked 信号,与 self.hello 绑定
        self.button.connect("clicked", self.hello, None)

        # This will cause the window to be destroyed by calling
        # gtk_widget_destroy(window) when "clicked".  Again, the destroy
        # signal could come from here, or the window manager.
        # 将 button 的 clicked 信号,与 gtk.Widget.destroy 绑定,对象是 self.windows
        self.button.connect_object("clicked", gtk.Widget.destroy, self.window)

        # This packs the button into the window (a GTK container).
        self.window.add(self.button)

        # The final step is to display this newly created widget.
        self.button.show()

        # and the window
        self.window.show()

    def main(self):
        # All PyGTK applications must have a gtk.main(). Control ends here
        # and waits for an event to occur (like a key press or mouse event).
        gtk.main()

# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) # 建立一个新的窗口

self.window.connect("destroy", self.destroy) #与一个已有的模块对接