== Manipulando imagens em pyGTK == Exemplo bem simples de como substituir uma imagem em uma área de desenho. '''Importante: Substituir os elementos de ''self.images'' em ''teste.py'' por imagens do filesystem local.''' Arquivo teste.glade {{{ True GDK_KEY_PRESS_MASK window1 GTK_WINDOW_TOPLEVEL GTK_WIN_POS_NONE False True False True False False GDK_WINDOW_TYPE_HINT_NORMAL GDK_GRAVITY_NORTH_WEST True True }}} Arquivo teste.py {{{ #!python import gtk, gtk.glade, random class MainWindow(object): def __init__(self,gladefile): # The main window widget tree self.wTree = gtk.glade.XML(gladefile, "mainwindow") # The main window self.mainwindow = self.wTree.get_widget("mainwindow") # The Drawing Area Widget self.draw = self.wTree.get_widget("drawingarea") self.images = ['img1.jpg','img2.jpg','img3.jpg','img4.jpg','img5.jpg'] # Signal and Event handlers self.signals_and_events() def signals_and_events(self): HANDLERS_AND_METHODS = { "on_mainwindow_delete_event": self.destroy, } # Event handling self.mainwindow.connect("key_press_event",self.paint) self.draw.connect("expose_event",self.paint) # Signal handling self.wTree.signal_autoconnect(HANDLERS_AND_METHODS) def paint(self,widget,event): areaw, areah = self.draw.window.get_size() style = self.draw.get_style() gc = style.fg_gc[gtk.STATE_NORMAL] # Get the image img = self.images[int(random.random() * 10) % len(self.images)] currpix = gtk.gdk.pixbuf_new_from_file(img) # Get the image buffer dimensions pw = currpix.get_width() ph = currpix.get_height() # Resize the drawing area self.draw.window.resize(pw,ph) # Scroll the window, if necessary self.draw.set_size_request(pw,ph) # Draw the pixbuf into the drawing area self.draw.window.draw_pixbuf(gc, currpix, 0, 0, 0, 0, pw,ph) def destroy(self, widget, event=None): gtk.main_quit() if __name__ == "__main__": app = MainWindow("teste.glade") gtk.main() }}}