## page was renamed from zhArticleTemplate ##language:zh #pragma section-numbers on ''' 含有章节索引的中文 文章模板 ''' ::-- ehu4ever [<>] <> = COME ON!! 继续前进吧 = == 对“信号处理”了解更多 == 我们再来注意一下connect(): {{{ object.connect(name, func, func_data) }}} 它的返回值是一具整数型变量,这个变量决定了程序之后的运行。单个的事件或是单个的widget都可以有n多的事件处理定义,它们会按你定义时的次序执行。 这个变量也可以用来取消一个事件处理定义。 {{{ object.disconnect(id) }}} 你也可以用singal_handler_block()和singal_handler_unblock()暂时地暂停对一种事件的处理。 {{{ object.signal_handler_block(handler_id) object.signal_handler_unblock(handler_id) }}} == Hello World的火力加强版 == {{{#!python #!/usr/bin/env python # example helloworld2.py import pygtk pygtk.require('2.0') import gtk class HelloWorld2: # Our new improved callback. The data passed to this method # is printed to stdout. def callback(self, widget, data): print "Hello again - %s was pressed" % data # another callback def delete_event(self, widget, event, data=None): gtk.main_quit() return False def __init__(self): # Create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) # This is a new call, which just sets the title of our # new window to "Hello Buttons!" self.window.set_title("Hello Buttons!") # Here we just set a handler for delete_event that immediately # exits GTK. self.window.connect("delete_event", self.delete_event) # Sets the border width of the window. self.window.set_border_width(10) # We create a box to pack widgets into. This is described in detail # in the "packing" section. The box is not really visible, it # is just used as a tool to arrange widgets. self.box1 = gtk.HBox(False, 0) # Put the box into the main window. self.window.add(self.box1) # Creates a new button with the label "Button 1". self.button1 = gtk.Button("Button 1") # Now when the button is clicked, we call the "callback" method # with a pointer to "button 1" as its argument self.button1.connect("clicked", self.callback, "button 1") # Instead of add(), we pack this button into the invisible # box, which has been packed into the window. self.box1.pack_start(self.button1, True, True, 0) # Always remember this step, this tells GTK that our preparation for # this button is complete, and it can now be displayed. self.button1.show() # Do these same steps again to create a second button self.button2 = gtk.Button("Button 2") # Call the same callback method with a different argument, # passing a pointer to "button 2" instead. self.button2.connect("clicked", self.callback, "button 2") self.box1.pack_start(self.button2, True, True, 0) # The order in which we show the buttons is not really important, but I # recommend showing the window last, so it all pops up at once. self.button2.show() self.box1.show() self.window.show() def main(): gtk.main() if __name__ == "__main__": hello = HelloWorld2() main() }}} 这个程序的运行结果如下图: ||Figure 3.1. Upgraded Hello World Example|| ||{{attachment:helloworld2.png}}|| 现在你应该发现想退出程序已经不是那么简单了,只能用窗口管理器或是命令行。你可以试着再在主窗口里加一个可以用于退出程序的QUIT按钮。 程序还有一个值得注意的地方是pack_start()。你可以试着改变窗口的大小。 下面我们来看一下这个版本的上一个版本的不同之处: 首先,这个版本里没有了destroy()这个callback。 13、14行定义了一个hello(),这个和上个版本相似。不同之处是它在console里输出的文本里有connect()中传入的数据。 27行为窗口设置了一个标题。 39行定义了一个水平方向的HBox,用来放置两个按钮。42行将这个HBox加到了主窗口中。 49到64行为按钮的“clicked”定义了事件处理,不同的按钮向callback传入不同的数据,这样在调用hello()的时候就会在console上输出不同的文本。 53和66将两个按钮pack进HBox中,57和70行是显示这两个按钮。 71、72行是显示HBox和主窗口。