helloworld.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # This example was adapted from http://pygtk.org/pygtk2tutorial/examples/helloworld.py
  3. import pygtk
  4. pygtk.require('2.0')
  5. import gtk
  6. class HelloWorld:
  7. def msgbox(self, text):
  8. "Display a simple message box"
  9. md = gtk.MessageDialog(self.window, gtk.DIALOG_DESTROY_WITH_PARENT,
  10. gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, text)
  11. md.run()
  12. # This is a callback function. The data arguments are ignored
  13. # in this example. More on callbacks below.
  14. def hello(self, widget, data=None):
  15. self.msgbox("Hello, world!")
  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. self.msgbox("delete event occurred")
  23. # Change FALSE to TRUE and the main window will not be destroyed
  24. # with a "delete_event".
  25. return False
  26. def destroy(self, widget, data=None):
  27. gtk.main_quit()
  28. def __init__(self):
  29. # create a new window
  30. self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  31. # When the window is given the "delete_event" signal (this is given
  32. # by the window manager, usually by the "close" option, or on the
  33. # titlebar), we ask it to call the delete_event () function
  34. # as defined above. The data passed to the callback
  35. # function is NULL and is ignored in the callback function.
  36. self.window.connect("delete_event", self.delete_event)
  37. # Here we connect the "destroy" event to a signal handler.
  38. # This event occurs when we call gtk_widget_destroy() on the window,
  39. # or if we return FALSE in the "delete_event" callback.
  40. self.window.connect("destroy", self.destroy)
  41. # Sets the border width of the window.
  42. self.window.set_border_width(10)
  43. # Creates a new button with the label "Hello World".
  44. self.button = gtk.Button("Hello World")
  45. # When the button receives the "clicked" signal, it will call the
  46. # function hello() passing it None as its argument. The hello()
  47. # function is defined above.
  48. self.button.connect("clicked", self.hello, None)
  49. # This will cause the window to be destroyed by calling
  50. # gtk_widget_destroy(window) when "clicked". Again, the destroy
  51. # signal could come from here, or the window manager.
  52. self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
  53. # This packs the button into the window (a GTK container).
  54. self.window.add(self.button)
  55. # The final step is to display this newly created widget.
  56. self.button.show()
  57. # and the window
  58. self.window.show()
  59. def main(self):
  60. # All PyGTK applications must have a gtk.main(). Control ends here
  61. # and waits for an event to occur (like a key press or mouse event).
  62. gtk.main()
  63. def main():
  64. hello = HelloWorld()
  65. hello.main()