含有章节索引的中文 文章模板
::-- ehu4ever [2005-08-08 09:13:23]
Contents
1. COME ON!! 继续前进吧
1.1. 对“信号处理”了解更多
我们再来注意一下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)
1.2. Hello World的火力加强版
1 #!/usr/bin/env python
2
3 # example helloworld2.py
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8
9 class HelloWorld2:
10
11 # Our new improved callback. The data passed to this method
12 # is printed to stdout.
13 def callback(self, widget, data):
14 print "Hello again - %s was pressed" % data
15
16 # another callback
17 def delete_event(self, widget, event, data=None):
18 gtk.main_quit()
19 return False
20
21 def __init__(self):
22 # Create a new window
23 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
24
25 # This is a new call, which just sets the title of our
26 # new window to "Hello Buttons!"
27 self.window.set_title("Hello Buttons!")
28
29 # Here we just set a handler for delete_event that immediately
30 # exits GTK.
31 self.window.connect("delete_event", self.delete_event)
32
33 # Sets the border width of the window.
34 self.window.set_border_width(10)
35
36 # We create a box to pack widgets into. This is described in detail
37 # in the "packing" section. The box is not really visible, it
38 # is just used as a tool to arrange widgets.
39 self.box1 = gtk.HBox(False, 0)
40
41 # Put the box into the main window.
42 self.window.add(self.box1)
43
44 # Creates a new button with the label "Button 1".
45 self.button1 = gtk.Button("Button 1")
46
47 # Now when the button is clicked, we call the "callback" method
48 # with a pointer to "button 1" as its argument
49 self.button1.connect("clicked", self.callback, "button 1")
50
51 # Instead of add(), we pack this button into the invisible
52 # box, which has been packed into the window.
53 self.box1.pack_start(self.button1, True, True, 0)
54
55 # Always remember this step, this tells GTK that our preparation for
56 # this button is complete, and it can now be displayed.
57 self.button1.show()
58
59 # Do these same steps again to create a second button
60 self.button2 = gtk.Button("Button 2")
61
62 # Call the same callback method with a different argument,
63 # passing a pointer to "button 2" instead.
64 self.button2.connect("clicked", self.callback, "button 2")
65
66 self.box1.pack_start(self.button2, True, True, 0)
67
68 # The order in which we show the buttons is not really important, but I
69 # recommend showing the window last, so it all pops up at once.
70 self.button2.show()
71 self.box1.show()
72 self.window.show()
73
74 def main():
75 gtk.main()
76
77 if __name__ == "__main__":
78 hello = HelloWorld2()
79 main()
这个程序的运行结果如下图:
Figure 3.1. Upgraded Hello World Example |
现在你应该发现想退出程序已经不是那么简单了,只能用窗口管理器或是命令行。你可以试着再在主窗口里加一个可以用于退出程序的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和主窗口。