- 论坛徽章:
- 0
|
1 #!/usr/bin/env python
2
3 # example helloworld.py
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8
9 class HelloWorld:
10
11 # This is a callback function. The data arguments are ignored
12 # in this example. More on callbacks below.
13 def hello(self, widget, data=None):
14 print "Hello World"
15
16 def delete_event(self, widget, event, data=None):
17 # If you return FALSE in the "delete_event" signal handler,
18 # GTK will emit the "destroy" signal. Returning TRUE means
19 # you don't want the window to be destroyed.
20 # This is useful for popping up 'are you sure you want to quit?'
21 # type dialogs.
22 print "delete event occurred"
23
24 # Change FALSE to TRUE and the main window will not be destroyed
25 # with a "delete_event".
26 return False
27
28 # Another callback
29 def destroy(self, widget, data=None):
30 gtk.main_quit()
31
32 def __init__(self):
33 # create a new window
34 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
35
36 # When the window is given the "delete_event" signal (this is given
37 # by the window manager, usually by the "close" option, or on the
38 # titlebar), we ask it to call the delete_event () function
39 # as defined above. The data passed to the callback
40 # function is NULL and is ignored in the callback function.
41 self.window.connect("delete_event", self.delete_event)
42
43 # Here we connect the "destroy" event to a signal handler.
44 # This event occurs when we call gtk_widget_destroy() on the window,
45 # or if we return FALSE in the "delete_event" callback.
46 self.window.connect("destroy", self.destroy)
47
48 # Sets the border width of the window.
49 self.window.set_border_width(10)
50
51 # Creates a new button with the label "Hello World".
52 self.button = gtk.Button("Hello World")
53
54 # When the button receives the "clicked" signal, it will call the
55 # function hello() passing it None as its argument. The hello()
56 # function is defined above.
57 self.button.connect("clicked", self.hello, None)
58
59 # This will cause the window to be destroyed by calling
60 # gtk_widget_destroy(window) when "clicked". Again, the destroy
61 # signal could come from here, or the window manager.
62 self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
63
64 # This packs the button into the window (a GTK container).
65 self.window.add(self.button)
66
67 # The final step is to display this newly created widget.
68 self.button.show()
69
70 # and the window
71 self.window.show()
72
73 def main(self):
74 # All PyGTK applications must have a gtk.main(). Control ends here
75 # and waits for an event to occur (like a key press or mouse event).
76 gtk.main()
77
78 # If the program is run directly or passed as an argument to the python
79 # interpreter then create a HelloWorld instance and show it
80 if __name__ == "__main__":
81 hello = HelloWorld()
82 hello.main()
![]()
最经典的Hello World程序。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/105486/showart_2095130.html |
|