Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 2.75 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2.  
  3. import pygtk
  4. pygtk.require('2.0')
  5. import gtk
  6.  
  7. class HelloWorld:
  8.     # This is a callback function. The data arguments are ignored
  9.     # in this example. More on callbacks below.
  10.    
  11.     def hello(self, widget, data=None):
  12.         print "Hello World"
  13.        
  14.     def delete_event(self, widget, event, data=None):
  15.         # If you return FALSE in the "delete_event" signal handler,
  16.         # GTK will emit the "destroy" signal. Returning TRUE means
  17.         # you don't want the window to be destroyed.
  18.         # This is useful for popping up 'are you sure you want to quit?'
  19.         # type dialogs.
  20.         print "delete event occured"
  21.        
  22.         # Change FALSE to TRUE and the main window will not be destroyed
  23.         # with a "delete_event".
  24.         return False
  25.        
  26.     def destroy(self, widget, data=None):
  27.         print "destroy signal occcured"
  28.         gtk.main_quit()
  29.        
  30.     def init(self):
  31.         # create a new window
  32.         self.window = gtk.window(gtk.WINDOW_TOPLEVEL)
  33.        
  34.         # When the window is given the "delete_event" signal (this is given
  35.         # by the window manager, usually by the "close" option, or on the
  36.         # titlebar), we ask it to call the delete_event () function
  37.         # as defined above. The data passed to the callback
  38.         # function is NULL and is ignored in the callback function.
  39.         self.window.connect("delete_event", self.delete_event)
  40.        
  41.         # Here we connect the "destroy" event to a signal handler.  
  42.         # This event occurs when we call gtk_widget_destroy() on the window,
  43.         # or if we return FALSE in the "delete_event" callback.
  44.         self.window.connect("destroy", self.destroy)
  45.        
  46.         # Sets the border width of the window
  47.         self.window.set_border_width(10)
  48.        
  49.         # Creates a new button with the label "Hello World."
  50.         self.button = gtk.Buttton("Hello World")
  51.        
  52.         # When the button recieves the "clicked" signal, it will call the
  53.         # function hello() passing it None as its argument. The hello()
  54.         # function is defined above.
  55.         self.button.connect("clicked", self.hello, None)
  56.        
  57.         # This will cause the window to be destroyed by calling
  58.         # gtk_widget_destroy(window) when "clicked". Again, the destroy
  59.         # signal could come from here, or the window manager.
  60.         self.button.connect("clicked", gtk.widget.destroy, self.window)
  61.        
  62.         # This packs the button into the window (a GTK container.)
  63.         self.window.add(self.button)
  64.        
  65.         # The final step is to display the newly created widget.
  66.         self.button.show
  67.         # and add the window
  68.         self.window.show
  69.        
  70.     def main(self):