~doctormo/python-snippets/lp-merge-request-example

« back to all changes in this revision

Viewing changes to pygtk/helloworld2.py

  • Committer: Jono Bacon
  • Date: 2009-12-31 01:32:01 UTC
  • Revision ID: jono@ubuntu.com-20091231013201-0jqe2hpla824dafl
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# [SNIPPET_NAME: Hello World 2]
 
4
# [SNIPPET_CATEGORIES: PyGTK]
 
5
# [SNIPPET_DESCRIPTION: Another hello world program]
 
6
 
 
7
# example helloworld2.py
 
8
 
 
9
import pygtk
 
10
pygtk.require('2.0')
 
11
import gtk
 
12
 
 
13
class HelloWorld2:
 
14
 
 
15
    # Our new improved callback.  The data passed to this method
 
16
    # is printed to stdout.
 
17
    def callback(self, widget, data):
 
18
        print "Hello again - %s was pressed" % data
 
19
 
 
20
    # another callback
 
21
    def delete_event(self, widget, event, data=None):
 
22
        gtk.main_quit()
 
23
        return False
 
24
 
 
25
    def __init__(self):
 
26
        # Create a new window
 
27
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
28
 
 
29
        # This is a new call, which just sets the title of our
 
30
        # new window to "Hello Buttons!"
 
31
        self.window.set_title("Hello Buttons!")
 
32
 
 
33
        # Here we just set a handler for delete_event that immediately
 
34
        # exits GTK.
 
35
        self.window.connect("delete_event", self.delete_event)
 
36
 
 
37
        # Sets the border width of the window.
 
38
        self.window.set_border_width(10)
 
39
 
 
40
        # We create a box to pack widgets into.  This is described in detail
 
41
        # in the "packing" section. The box is not really visible, it
 
42
        # is just used as a tool to arrange widgets.
 
43
        self.box1 = gtk.HBox(False, 0)
 
44
 
 
45
        # Put the box into the main window.
 
46
        self.window.add(self.box1)
 
47
 
 
48
        # Creates a new button with the label "Button 1".
 
49
        self.button1 = gtk.Button("Button 1")
 
50
 
 
51
        # Now when the button is clicked, we call the "callback" method
 
52
        # with a pointer to "button 1" as its argument
 
53
        self.button1.connect("clicked", self.callback, "button 1")
 
54
 
 
55
        # Instead of add(), we pack this button into the invisible
 
56
        # box, which has been packed into the window.
 
57
        self.box1.pack_start(self.button1, True, True, 0)
 
58
 
 
59
        # Always remember this step, this tells GTK that our preparation for
 
60
        # this button is complete, and it can now be displayed.
 
61
        self.button1.show()
 
62
 
 
63
        # Do these same steps again to create a second button
 
64
        self.button2 = gtk.Button("Button 2")
 
65
 
 
66
        # Call the same callback method with a different argument,
 
67
        # passing a pointer to "button 2" instead.
 
68
        self.button2.connect("clicked", self.callback, "button 2")
 
69
 
 
70
        self.box1.pack_start(self.button2, True, True, 0)
 
71
 
 
72
        # The order in which we show the buttons is not really important, but I
 
73
        # recommend showing the window last, so it all pops up at once.
 
74
        self.button2.show()
 
75
        self.box1.show()
 
76
        self.window.show()
 
77
 
 
78
def main():
 
79
    gtk.main()
 
80
 
 
81
if __name__ == "__main__":
 
82
    hello = HelloWorld2()
 
83
    main()