3
# [SNIPPET_NAME: Hello World 2]
4
# [SNIPPET_CATEGORIES: PyGTK]
5
# [SNIPPET_DESCRIPTION: Another hello world program]
7
# example helloworld2.py
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
21
def delete_event(self, widget, event, data=None):
27
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
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!")
33
# Here we just set a handler for delete_event that immediately
35
self.window.connect("delete_event", self.delete_event)
37
# Sets the border width of the window.
38
self.window.set_border_width(10)
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)
45
# Put the box into the main window.
46
self.window.add(self.box1)
48
# Creates a new button with the label "Button 1".
49
self.button1 = gtk.Button("Button 1")
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")
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)
59
# Always remember this step, this tells GTK that our preparation for
60
# this button is complete, and it can now be displayed.
63
# Do these same steps again to create a second button
64
self.button2 = gtk.Button("Button 2")
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")
70
self.box1.pack_start(self.button2, True, True, 0)
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.
81
if __name__ == "__main__":